我在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或者甚至
浮动:正确的.
我还找到了另一种使用表格布局和表格行的解决方案,但我需要一个按钮来触发一些动作.我不希望文本的两个部分有两个按钮.
提前致谢.
解决方法:
第1步:res / drawable / button_selector.xml
第2步:使用两个TextView创建一个LinearLayout.
android:id="@+id/myCustomButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/button_selector" >
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" />
android:id="@+id/aiResetPwdButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:textColor="@color/blue"/>
第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();
}
});
标签:android,android-layout
来源: https://codeday.me/bug/20190708/1405351.html