[英]Button with text aligned left and right
我在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
。
我还找到了另一种使用表格布局和表格行的解决方案,但我需要一个按钮来触发一些动作。 我不希望文本的两个部分有两个按钮。
提前致谢。
第1步: 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();
}
});
不要使用按钮,使用RelativeLayout
(甚至是LinearLayout可以做到)和两个textviews
。 给ihat布局一个背景,使它看起来像一个按钮。 然后,将OnClickListener
添加到布局中。
使用重力有什么问题吗?
android:gravity="right|center_vertical"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.