文本左右对齐的按钮

Gre*_*reg 5 android android-layout

我是 android 开发的新手,我需要你的帮助。我需要带有与左侧对齐的粗体文本的按钮,以及与右侧对齐的具有不同字体(较小和不同颜色)的另一个文本,如下所示:

[**Name**           john]
//Name is bold and black
// john is smaller and let's say blue
Run Code Online (Sandbox Code Playgroud)

我在网上做了一个研究,但我找不到任何东西。我试过:

this.button.setText(Html.fromHtml(styledText));
Run Code Online (Sandbox Code Playgroud)

但看起来它不适用于alignment="right",text-allign:right甚至 float:right.

我还找到了另一种使用表格布局和表格行的解决方案,但我需要一个按钮来触发某些操作。我不希望文本的两个部分有两个按钮。

提前致谢。

vis*_*dra 2

第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>
Run Code Online (Sandbox Code Playgroud)

步骤2:创建一个LinearLayout和两个TextView。

           <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>
Run Code Online (Sandbox Code Playgroud)

第 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();
    }
});
Run Code Online (Sandbox Code Playgroud)