Android:如何在同一个按钮中更改两条不同线条的颜色?
我最近正试图为我朋友的应用程序构建一个用户界面。 我遇到了一些问题,不知道如何处理。 我已经寻找了一段时间的解决方案。 但还是不能解决我的问题 我想知道是否有办法改变中间按钮列中两个文本的颜色。Android:如何在同一个按钮中更改两条不同线条的颜色?,android,Android,我最近正试图为我朋友的应用程序构建一个用户界面。 我遇到了一些问题,不知道如何处理。 我已经寻找了一段时间的解决方案。 但还是不能解决我的问题 我想知道是否有办法改变中间按钮列中两个文本的颜色。 我想要的是:数字是粉红色的,中文是灰色的,还有两种不同的大小。最好的选择是按照萨法在评论中的建议将两个文本分成两个文本视图,埃尔维拉·帕帕拉克回答 如果您确实希望将两个文本都保存在一个按钮中,可以使用with 可以使用LinearLayout创建自定义按钮。您只需将android:clickable=
我想要的是:数字是粉红色的,中文是灰色的,还有两种不同的大小。最好的选择是按照萨法在评论中的建议将两个文本分成两个文本视图,埃尔维拉·帕帕拉克回答 如果您确实希望将两个文本都保存在一个按钮中,可以使用with
可以使用LinearLayout创建自定义按钮。您只需将android:clickable=true属性设置为LinearLayout。以下是您案例的一个示例:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/buttonList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/layout1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/button_selector"
android:clickable="true"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="22"
android:textColor="@color/bp_disabled_day" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="A"
android:textColor="@color/ampm_text_color" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="fill_parent"
android:background="@android:color/darker_gray" />
<LinearLayout
android:id="@+id/layout2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/button_selector"
android:clickable="true"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="22"
android:textColor="@color/bp_disabled_day" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="B"
android:textColor="@color/ampm_text_color" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="fill_parent"
android:background="@android:color/darker_gray" />
<LinearLayout
android:id="@+id/layout3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/button_selector"
android:clickable="true"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="22"
android:textColor="@color/bp_disabled_day" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="C"
android:textColor="@color/ampm_text_color" />
</LinearLayout>
</LinearLayout>
最后一步是在代码中设置OnclickListener:
LinearLayout layout1 = (LinearLayout) findViewById (R.id.layout1);
layout1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO: Write your code
}
});
结果是:
您可以将xml发布到您的问题上…更改按钮,并使用带有两个文本视图的LinearLayout,您可以根据需要自定义文本。
<?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/bpWhite" android:state_focused="false" android:state_pressed="false" />
<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" />
</selector>
LinearLayout layout1 = (LinearLayout) findViewById (R.id.layout1);
layout1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO: Write your code
}
});