我想通过以下方式对齐按钮上的文本:
Thickness 20mm
I want thickness to be aligned to the left and the value(20mm) to be aligned to the right of the button. I initially went with something like
thickness_button.setText(res.getString(R.string.THICKNESS)
+"\t\t\t\t\t\t\t\t\t\t"+value);
这最初有效,但是当我更改区域设置时,THICKNESS
字符串相应地发生变化并且非常长,因此该值被推送到下一行,如下所示:
Thickness 20mm
如何解决这个问题?有没有不涉及更改 UI 元素(按钮)的解决方案?
最佳答案
按钮与 TextView/View 没有太大区别。我建议不要使用 Button 并使用带有 2 个 TextView 的 FrameLayout,布局重力为左和右。
只需在 FrameLayout 上添加一个点击监听器,您就构建了一个按钮。
frameLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//...
}
});
<FrameLayout
android:id="@+id/mybutton"
android:layout_width="150dp"
android:layout_height="50dp">
<TextView
android:id="@+id/mytext"
android:textColor="#ff000000"
android:layout_gravity="center_vertical|left"
android:text="XXX"
/>
<TextView
android:id="@+id/myvalue"
android:textColor="#ff000000"
android:layout_gravity="center_vertical|right"
android:text="YYY"
/>
/>
关于java - 如何对齐按钮上的部分文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8489668/