简体   繁体   English

为相对布局创建“受压状态”

[英]Create a “ Pressed State” for a relative layout

I have build this Dashboard(See image Attached) 我已经构建了该仪表板(请参见附件) 在此处输入图片说明

What I am trying to do is create a "Press State" for the buttons.A colour change would work fine for me. 我想做的是为按钮创建一个“ Press State”(按下状态)。颜色更改对我来说很好。

The dashboard buttons are made of 5 Relative layouts where each one holds two image views and two textviews. 仪表板按钮由5个相对布局组成,每个布局包含两个图像视图和两个文本视图。

The code is listed below just for one button in this case "Mobile Apps" : 下面仅针对一个按钮列出了该代码,在这种情况下为“ Mobile Apps”:

<RelativeLayout
            android:id="@+id/appsHolder"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="@drawable/gradient_background"
            android:padding="5dp" >

            <!-- ListRow Left sied Thumbnail image -->

            <LinearLayout
                android:id="@+id/thumbnail"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_marginRight="5dip"
                android:padding="3dip" >

                <ImageView
                    android:id="@+id/list_image"
                    android:layout_width="150dip"
                    android:layout_height="150dip"
                    android:src="@drawable/ic_apps_dashboard" />
            </LinearLayout>

            <!-- Title Of Song -->

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/thumbnail"
                android:layout_toRightOf="@+id/thumbnail"
                android:text="Mobile Apps"
                android:textColor="#000000"
                android:textSize="30dip"
                android:textStyle="bold"
                android:typeface="sans" />

            <!-- Artist Name -->

            <TextView
                android:id="@+id/subtitle"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/title"
                android:layout_marginTop="1dip"
                android:layout_toRightOf="@+id/thumbnail"
                android:text="Preview the Viessmann apps..."
                android:textColor="#000000"
                android:textSize="30dip" />

            <!-- Rightend Duration -->


            <!-- Rightend Arrow -->

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:src="@drawable/ic_arrow_dashboard" />
        </RelativeLayout>

ANY GUIDANCE WILL BE APPRECIATED! 任何指导将被认可!

You have to write your own selector: 您必须编写自己的选择器:

http://developer.android.com/guide/topics/resources/color-list-resource.html http://developer.android.com/guide/topics/resources/color-list-resource.html

Create a xml file in your res/drawable/ folder and apply it as background to your button: 在res / drawable /文件夹中创建一个xml文件,并将其作为背景应用于按钮:

my_background_selector.xml: my_background_selector.xml:

<?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/background_gradient_pressed" />
   <item android:state_focused="true" android:drawable="@drawable/background_gradient_pressed" />
   <item android:state_selected="true" android:drawable="@drawable/background_gradient_pressed" />


   <item android:drawable="@drawable/background_gradient" />

 </selector>

Then simply apply the selector as background on your layout 然后只需将选择器作为背景应用于布局

<RelativeLayout
            android:id="@+id/appsHolder"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="@drawable/my_background_selector"
            android:padding="5dp" 
    ...
</RelativeLayout>

If you want to work with colors, you can also do something like this: 如果要使用颜色,还可以执行以下操作:

<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">

   <item android:state_pressed="true">
       <shape>
         <solid android:color="@color/pressed_color" />
       </shape>
   </item>


   <item>
     <shape>
        <solid android:color="#B8B8B8" />
     </shape>
   </item>

 </selector>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM