我在xml中使用了线性布局,并通过Java向该线性布局添加另一个包含两个文本视图的线性布局。触摸事件工作正常,但我想通过设置背景颜色来突出显示所选的线性布局。请给予建议。
我在xml中使用了线性布局,并通过Java向该线性布局添加另一个包含两个文本视图的线性布局。触摸事件工作正常,但我想通过设置背景颜色来突出显示所选的线性布局。请给予建议。
在drawable文件夹中定义background.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/pressed" />
<item android:state_focused="false"
android:drawable="@drawable/normal" />
</selector>
drawable文件夹中的normal.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
</shape>
在drawable文件夹中的pressed.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FF1A47"/>
</shape>
然后将背景设置为您的布局。
android:background="@drawable/background"
你也可以像下面这样设置背景色: ll.setOnTouchListener( new View.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
ll.setBackgroundColor(Color.RED);
break;
case MotionEvent.ACTION_UP:
//set color back to default
ll.setBackgroundColor(Color.WHITE);
break;
}
return true;
}
});
LinearLayout
可点击:
XML:
android:clickable="true"
代码:
myLinearLayout.setClickable(true);
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/bg_pressed_tab"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/app_name"
android:src="@mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/service_request"
android:textColor="@color/white"
android:textSize="@dimen/text_size_small" />
<requestFocus />
</LinearLayout>
bg_tab_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="@android:integer/config_longAnimTime">
<item android:drawable="@color/colorPrimaryDark" android:state_focused="true" android:state_pressed="true" />
<item android:drawable="@color/colorPrimaryDark" android:state_focused="false" android:state_pressed="true" />
<item android:drawable="@color/colorPrimaryDark" android:state_focused="true" android:state_pressed="false" />
<item android:drawable="@color/colorPrimary" android:state_focused="false" android:state_pressed="false" />
</selector>
public static void setClickableAnimation(final Context context, final View view) {
final int[] attrs = new int[]{R.attr.selectableItemBackground};
final TypedArray typedArray = context.obtainStyledAttributes(attrs);
final int backgroundResource = typedArray.getResourceId(0, 0);
view.setBackgroundResource(backgroundResource);
typedArray.recycle();
}
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/list_focused" android:state_pressed="true"/>
<item android:drawable="@drawable/list_focused" android:state_enabled="true" android:state_focused="true" android:state_window_focused="true"/>
<item android:drawable="@drawable/list_raw_img"/>
</selector>
在xml文件中设置线性布局的背景
android:background="@drawable/background"
View
函数setBackgroundColor()
请参阅Android文档:
http://developer.android.com/reference/android/view/View.html#setBackgroundColor%28int%29
public void setBackgroundColor (int color) Added in API level 1
Sets the background color for this view.
Parameters
color the color of the background
LinearLayouts
实现触摸或点击事件,并根据布局的选择更改其背景颜色。 - GrIsHu