我做了一个按钮,改变了不同状态下的背景,这样:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/btn_location_pressed" /> <!-- pressed -->
<item android:state_focused="true" android:drawable="@drawable/btn_location_pressed"/> <!-- focused -->
<item android:drawable="@drawable/btn_location"/> <!-- default -->
Run Code Online (Sandbox Code Playgroud)
这里的问题是我也试图改变textColor,就像我对drawable一样,但我无法做到.我已经尝试过android:textColor和android:color但是第一个不起作用,而秒数改变了我的背景.
下一个代码是我布局的一部分.关于文本颜色,它仅适用于正常状态文本颜色,因此在按下时不会将其更改为白色
<Button android:id="@+id/location_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:background="@drawable/location"
android:textSize="15sp"
android:textColor="@color/location_color"
android:textColorHighlight="#FFFFFF"
/>
Run Code Online (Sandbox Code Playgroud)
有人有线索吗?
我从XML获得了一个视图,其代码如下:
Button view = (Button) LayoutInflater.from(this).inflate(R.layout.section_button, null);
Run Code Online (Sandbox Code Playgroud)
我想为按钮设置一个"样式"我怎么能在java中这样做,因为我想使用几个样式我将使用的每个按钮.
我通过一些按钮以编程方式为AlertDialog创建LinearLayout.
我WANT做到这一点:
<LinearLayout android:id="@+id/footer" android:layout_width="fill_parent"
style="@android:style/ButtonBar">
Run Code Online (Sandbox Code Playgroud)
但是使用这样的代码:
LinearLayout buttons = new LinearLayout(parentContext);
buttons.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams buttonsParams =
new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
topLayout.addView(buttons, buttonsParams);
buttons.setLayoutParams(buttonsParams);
Button btnAdd = new Button(context);
btnAdd.setText("Add");
Run Code Online (Sandbox Code Playgroud)
如何以编程方式设置按钮的样式(使用按钮栏)?
我试图以编程方式创建一些按钮将它们添加到现有的ViewGroup.这工作正常,但我无法使用正确的样式初始化按钮.
我知道,创建后无法设置/更改视图样式.但到目前为止我发现的所有解决方案都说,使用自定义样式创建视图应该没问题(从我认为的API级别11开始,我使用的是14+):
Button button = new Button (getActivity(), null, R.style.MyButtonStyle);
Run Code Online (Sandbox Code Playgroud)
唯一的效果是,按钮创建时没有任何样式.没有背景,所以选择器,没有边距/填充只是纯文本.我认为MyButtonStyle会被破坏,但是使用MyButtonStyle在XML中创建按钮没问题.
为什么这不起作用?