Jas*_*ado 97 android android-layout
我有一个LinearLayout,我的样式看起来像一个按钮,它包含一些text/ImageView元素.我想让整个LinearLayout像一个按钮一样,特别是给它定义的状态,因此它在按下时具有不同的背景.
有没有比将ImageButton设置为整个布局和定位大小更好的方法?
Fab*_*biF 158
如果您想添加Android默认背景行为以使其Layout
行为像"clikable" View
,请设置目标Layout
:
android:background="?android:attr/selectableItemBackground"
Run Code Online (Sandbox Code Playgroud)
android:background="?attr/selectableItemBackground"
Run Code Online (Sandbox Code Playgroud)
android:background="@android:drawable/list_selector_background"
Run Code Online (Sandbox Code Playgroud)
上面的答案仍然是真的,但没有帮助我只是添加默认的按下和释放的UI状态(如在ListView
例如).
Roc*_*off 152
我刚才遇到了这个问题.您必须将LinearLayout设置为可单击.您可以在XML中执行此操作
android:clickable="true"
Run Code Online (Sandbox Code Playgroud)
或者在代码中
yourLinearLayout.setClickable(true);
Run Code Online (Sandbox Code Playgroud)
干杯!
小智 15
我使用了第一个和第二个答案.但我的linearlayout有图像和背景颜色的文字,所以我不得不将"背景"更改为"前景"
的LinearLayout
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
Run Code Online (Sandbox Code Playgroud)
首先,您需要一个选择器来定义不同的状态.例如,在XML文件中:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_pressed"
android:state_pressed="true" />
<item android:drawable="@drawable/button_focused"
android:state_focused="true" />
<item android:drawable="@drawable/button_normal" />
</selector>
Run Code Online (Sandbox Code Playgroud)
我没有尝试过,但你可以将LinearLayout的android:background设置为这个选择器,并将android:clickable设置为true并且它将起作用.
如果没有,您可以切换到使用RelativeLayout,并使第一个元素成为一个按钮,该选择器作为背景,fill_parent作为其布局宽度和高度.在这种情况下,只需使用常规Button并将android:background设置为您的选择器.您不必在按钮上放置文本.
在我正在工作的应用程序中,我需要动态创建LinearLayout.在这种情况下命令
ll.setClickable(true);
Run Code Online (Sandbox Code Playgroud)
没有按照预期的方式工作.虽然我可能会错过一些东西,但我设法利用setOnTouchListener来获得相同的结果,并且我提交代码以防任何人有相同的需求.
以下代码创建一个LinearLayout,其中包含两个textview和圆角,按下时会更改颜色.
首先,在drawable文件夹中创建两个xml文件,一个用于普通文件,一个用于压缩线性布局状态.
正常状态xml(drawable/rounded_edges_normal.xml)
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#FFFFFF" />
<corners android:radius="7dp" />
<padding android:left="5dip" android:top="5dip" android:right="5dip" android:bottom="5dip" />
</shape>
</item>
<item android:bottom="3px">
<shape android:shape="rectangle">
<solid android:color="#F1F1F1" />
<corners android:radius="7dp" />
</shape>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
按下状态xml(drawable/rounded_edges_pressed.xml).唯一的区别在于颜色......
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#FFFFFF" />
<corners android:radius="7dp" />
<padding android:left="5dip" android:top="5dip" android:right="5dip" android:bottom="5dip" />
</shape>
</item>
<item android:bottom="3px">
<shape android:shape="rectangle">
<solid android:color="#add8e6" />
<corners android:radius="7dp" />
</shape>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
然后,以下代码完成工作
全局变量:
public int layoutpressed = -1;
Run Code Online (Sandbox Code Playgroud)
在onCreate()
:
// Create some textviews to put into the linear layout...
TextView tv1 = new TextView(this);
TextView tv2 = new TextView(this);
tv1.setText("First Line");
tv2.setText("Second Line");
// LinearLayout definition and some layout properties...
final LinearLayout ll = new LinearLayout(context);
ll.setOrientation(LinearLayout.VERTICAL);
// it is supposed that the linear layout will be in a table.
// if this is not the case for you change next line appropriately...
ll.setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
ll.setBackgroundResource(R.drawable.rounded_edges_normal);
ll.addView(tv1);
ll.addView(tv2);
ll.setPadding(10, 10, 10, 10);
// Now define the three button cases
ll.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if (arg1.getAction()==MotionEvent.ACTION_DOWN){
ll.setBackgroundResource(R.drawable.rounded_edges_pressed);
ll.setPadding(10, 10, 10, 10);
layoutpressed = arg0.getId();
}
else if (arg1.getAction()== MotionEvent.ACTION_UP){
ll.setBackgroundResource(R.drawable.rounded_edges_normal);
ll.setPadding(10, 10, 10, 10);
if(layoutpressed == arg0.getId()){
// ...........................................................................
// Code to execute when LinearLayout is pressed...
// ...........................................................................
}
}
else{
ll.setBackgroundResource(R.drawable.rounded_edges_showtmimata);
ll.setPadding(10, 10, 10, 10);
layoutpressed = -1;
}
return true;
}
});
Run Code Online (Sandbox Code Playgroud)
只需设置这些属性
<LinearLayout
...
android:background="@android:drawable/btn_default"
android:clickable="true"
android:focusable="true"
android:onClick="onClick"
>
...
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
只需将背景属性/selectableItemBackground 添加到线性布局,并使该线性布局可点击
例子 :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linear1"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true">
Run Code Online (Sandbox Code Playgroud)
这使得线性布局在按下时就像按钮一样。:)
归档时间: |
|
查看次数: |
81856 次 |
最近记录: |