dwb*_*ito 245 android android-button android-selector
我做了一个按钮,改变了不同状态下的背景,这样:
<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)
有人有线索吗?
Kon*_*rov 563
为您的按钮创建有状态颜色,就像您为背景所做的那样,例如:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Focused and not pressed -->
<item android:state_focused="true"
android:state_pressed="false"
android:color="#ffffff" />
<!-- Focused and pressed -->
<item android:state_focused="true"
android:state_pressed="true"
android:color="#000000" />
<!-- Unfocused and pressed -->
<item android:state_focused="false"
android:state_pressed="true"
android:color="#000000" />
<!-- Default color -->
<item android:color="#ffffff" />
</selector>
Run Code Online (Sandbox Code Playgroud)
将xml放在res/drawable文件夹中的文件中,即res/drawable/button_text_color.xml.然后将drawable设置为文本颜色:
android:textColor="@drawable/button_text_color"
Run Code Online (Sandbox Code Playgroud)
Mic*_*ski 16
另一种方法是在你的班上:
import android.graphics.Color; // add to top of class
Button btn = (Button)findViewById(R.id.btn);
// set button text colour to be blue
btn.setTextColor(Color.parseColor("blue"));
// set button text colour to be red
btn.setTextColor(Color.parseColor("#FF0000"));
// set button text color to be a color from your resources (could be strings.xml)
btn.setTextColor(getResources().getColor(R.color.yourColor));
// set button background colour to be green
btn.setBackgroundColor(Color.GREEN);
Run Code Online (Sandbox Code Playgroud)
小智 5
好的,非常简单,首先转到 1. res-value 并打开 colors.xml 2.copy 1 定义的文本,例如 #FF4081 并更改名称,例如我更改为白色并更改其值,例如我更改为 #FFFFFF像这样的白色值
<color name="White">#FFFFFF</color>
Run Code Online (Sandbox Code Playgroud)
然后在你的按钮内添加这一行
b3.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.White));
Run Code Online (Sandbox Code Playgroud)
好的,b3 是我的按钮的名称,因此更改了您按钮的名称,如果您使用白色,如果您更改不同的颜色,然后将白色更改为您的颜色名称,但首先您必须在颜色中定义该颜色。 xml 就像我在 pont 2 中解释的那样
| 归档时间: |
|
| 查看次数: |
194948 次 |
| 最近记录: |