Arbeiten mit Selector

  • 2 Antworten
  • Letztes Antwortdatum
M

MaikB85

Gast
Hallo

Ich möchte mir eine Art Menü bauen welches aus einem LinearLayout besteht und mehrere Elemente (Menüpunkte) hat. Beim Klick auf ein Element soll sich dessen Design etwas ändern (also sich wie Buttons verhalten), sodass es nachher in etwa so aussieht:
menu.jpg


Ich hab das schon fast komplett so umgesetzt indem ich mit selector arbeite im xml file. Was ich gerne noch hätte ist dieser blaue Balken beim markiertem Element. Ist das auch über xml möglich?

Menülayout:
Hier mal meine Layout Dateien:
Code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#535552">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/LinearLayout_item1"
        android:background="@drawable/itemclicked"
        android:clickable="true"
        android:focusable="true">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView"
            android:src="@mipmap/ic_wb_incandescent_black_48dp"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:tint="#dfdfdf" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="@string/details_menu_light"
            android:id="@+id/textView"
            android:layout_marginTop="12dp"
            android:layout_marginBottom="12dp"
            android:layout_marginLeft="12dp"
            android:layout_gravity="center_vertical"
            android:textColor="#dfdfdf" />
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#dfdfdf"
        android:id="@+id/divider1"></View>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/LinearLayout_item2"
        android:background="@drawable/itemclicked"
        android:clickable="true"
        android:focusable="true">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView2"
            android:src="@mipmap/ic_toys_black_48dp"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:tint="#dfdfdf" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="@string/details_menu_stream"
            android:id="@+id/textView2"
            android:layout_marginTop="12dp"
            android:layout_marginBottom="12dp"
            android:layout_marginLeft="12dp"
            android:layout_gravity="center_vertical"
            android:textColor="#dfdfdf" />
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#dfdfdf"
        android:id="@+id/divider2"></View>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/LinearLayout_item3"
        android:background="@drawable/itemclicked"
        android:clickable="true"
        android:focusable="true">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView3"
            android:src="@mipmap/ic_poll_black_48dp"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:tint="#dfdfdf" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="@string/details_menu_watervalues"
            android:id="@+id/textView3"
            android:layout_marginTop="12dp"
            android:layout_marginBottom="12dp"
            android:layout_marginLeft="12dp"
            android:layout_gravity="center_vertical"
            android:textColor="#dfdfdf" />
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#dfdfdf"
        android:id="@+id/divider3"></View>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/LinearLayout_item4"
        android:background="@drawable/itemclicked"
        android:clickable="true"
        android:focusable="true">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView4"
            android:src="@mipmap/ic_colorize_black_48dp"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:tint="#dfdfdf" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="@string/details_menu_dosage"
            android:id="@+id/textView4"
            android:layout_marginTop="12dp"
            android:layout_marginBottom="12dp"
            android:layout_marginLeft="12dp"
            android:layout_gravity="center_vertical"
            android:textColor="#dfdfdf" />
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#dfdfdf"
        android:id="@+id/divider4"></View>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/LinearLayout_item5"
        android:background="@drawable/itemclicked"
        android:clickable="true"
        android:focusable="true">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView5"
            android:src="@mipmap/ic_settings_black_48dp"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:tint="#dfdfdf" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="@string/details_menu_automation"
            android:id="@+id/textView5"
            android:layout_marginTop="12dp"
            android:layout_marginBottom="12dp"
            android:layout_marginLeft="12dp"
            android:layout_gravity="center_vertical"
            android:textColor="#dfdfdf" />
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#dfdfdf"
        android:id="@+id/divider5"></View>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/LinearLayout_item6"
        android:background="@drawable/itemclicked"
        android:clickable="true"
        android:focusable="true">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView6"
            android:src="@mipmap/ic_format_list_bulleted_black_48dp"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:tint="#dfdfdf" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="@string/details_menu_maintenance"
            android:id="@+id/textView6"
            android:layout_marginTop="12dp"
            android:layout_marginBottom="12dp"
            android:layout_marginLeft="12dp"
            android:layout_gravity="center_vertical"
            android:textColor="#dfdfdf" />
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#dfdfdf"
        android:id="@+id/divider6"></View>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/LinearLayout_item7"
        android:background="@drawable/itemclicked"
        android:clickable="true"
        android:focusable="true">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView7"
            android:src="@mipmap/ic_message_black_48dp"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:tint="#dfdfdf" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="@string/details_menu_alarmmessages"
            android:id="@+id/textView7"
            android:layout_marginTop="12dp"
            android:layout_marginBottom="12dp"
            android:layout_marginLeft="12dp"
            android:layout_gravity="center_vertical"
            android:textColor="#dfdfdf" />
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#dfdfdf"
        android:id="@+id/divider7"></View>
</LinearLayout>
</ScrollView>

itemclicked.xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
        android:drawable="@drawable/selected" />
    <item  android:state_focused="false"
        android:drawable="@drawable/unselected" />
</selector>

selected.xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#434542"/>
</shape>

unselected.xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#535552"/>
</shape>

Hoffe mir kann da jemand weiterhelfen...

Gruß Maik
 
wieso machst du diesen blauen balken nciht einfach in dein drawable mit rein?
 
Ja wollte ich ja, ich weiß nur nicht genau wie. Ich weiß nicht wie ich da so einen Balken zeichnen kann
 
Zurück
Oben Unten