Android 同时选择ImageButton和TextView
我想知道是否有一种方法可以同时选择(点击时突出显示)ImageButton和TextView。我有一个图像按钮和文本视图。我将它们放在一个RelativeLayout中,并将背景设置为一个可绘制的选择器xml。但它们并不在一起。如果我按下ImageButton按钮,它将单独高亮显示;如果我按下布局,它将高亮显示,而不高亮显示ImageButton。你能在这方面帮助我吗Android 同时选择ImageButton和TextView,android,android-layout,Android,Android Layout,我想知道是否有一种方法可以同时选择(点击时突出显示)ImageButton和TextView。我有一个图像按钮和文本视图。我将它们放在一个RelativeLayout中,并将背景设置为一个可绘制的选择器xml。但它们并不在一起。如果我按下ImageButton按钮,它将单独高亮显示;如果我按下布局,它将高亮显示,而不高亮显示ImageButton。你能在这方面帮助我吗 谢谢。为TextView和ImageView的父布局使用自定义背景 <?xml version="1.0" encodin
谢谢。为TextView和ImageView的父布局使用自定义背景
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="@drawable/listview_background_focused" />
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/listview_background_focused" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="@drawable/listview_background_focused" />
<item
android:drawable="@drawable/listview_background_unfocused" />
</selector>
并在父布局的onclickListener中编写相应的代码,而不是在子视图中编写。我建议声明一个包含如下文本和图像的按钮
<Button
android:id="@+id/dash_bt_list"
android:drawableTop="@drawable/your_image"
android:background="@drawable/selector"
android:onClick="onClickFeature"
android:text="@string/your_text"
android:layout_width="wrap_content" />
在选择器中:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true">
<shape>
<solid android:color="#FFFFFF" />
<stroke android:width="3dp" android:color="#000000" />
<padding android:bottom="1dp" android:left="1dp" android:right="1dp"
android:top="1dp" />
</shape>
...
</selector>
...
这对我很有帮助我有解决方案给你
- 将
和“TextView”包装在“LinearLayout”中ImageButton
- 禁用Imagebutton和textview的可点击事件、可聚焦事件
- 为您的
线性布局应用自定义选择器
custom_component.xml
layout文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#f0f0f0"
android:gravity="center"
android:orientation="horizontal"
android:padding="10dp" >
<LinearLayout
android:id="@+id/myCustomComponent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/layout_bgimage_selector"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:orientation="vertical" >
<ImageButton
android:id="@+id/testImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="@drawable/icon" />
<TextView
android:id="@+id/testText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Test Text"
android:textColor="#FFF" />
</LinearLayout>
</RelativeLayout>
我用于布局选择器的9面片图像包括:
背景:1.9.png-
背景_over.9.png-
将它们放在
res/drawable hdpi
文件夹中。(或与您的设备配置相匹配的任何图像资源文件夹)您是否可以发布您的操作侦听器代码确保您的RelativeLayout
可单击,并将onClickListener分配给您的布局,而不是在文本或图像视图上。
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/background" android:state_enabled="false"/>
<item android:drawable="@drawable/background" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="@drawable/background_over" android:state_enabled="true" android:state_pressed="true"/>
<item android:drawable="@drawable/background_over" android:state_enabled="true" android:state_focused="true"/>
<item android:drawable="@drawable/background" android:state_enabled="true"/>
</selector>