我是 android 开发的新手,我需要你的帮助。我需要带有粗体文本的按钮与左侧对齐,而另一个具有不同字体(较小和不同颜色)的文本与右侧对齐,类似这样:
[**Name** john]
//Name is bold and black
// john is smaller and let's say blue
我在网上进行了研究,但找不到任何东西。我试过:
this.button.setText(Html.fromHtml(styledText));
但看起来它不适用于 alignment="right"
、text-allign:right
甚至float:right
.
我还找到了另一种使用表格布局和表格行的解决方案,但我需要一个按钮来触发一些操作。我不希望文本的两个部分有两个按钮。
提前致谢。
第一步:res/drawable/button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_bg_pressed" android:state_pressed="true"></item>
<item android:drawable="@drawable/button_bg_normal"></item>
</selector>
第 2 步:创建一个带有两个 TextView 的 LinearLayout。
<LinearLayout
android:id="@+id/myCustomButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/button_selector" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textStyle="bold"
android:textColor="@color/black"
android:background="@drawable/ai_contact_us" />
<TextView
android:id="@+id/aiResetPwdButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:textColor="@color/blue"/>
</LinearLayout>
第 3 步:在您的 onCreate() 中
View buttonView = findViewById(R.id.myCustomButton);
buttonView.setOnClickListener(new View.OnClickListenet(){
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();
}
});
我是一名优秀的程序员,十分优秀!