我正在尝试更改按钮内文本的重力。知道按钮是在 ConstraintLayout 中定义的,如下所示:
<Button
android:id="@+id/x"
android:layout_width="0dp"
android:layout_height = "0dp"
app:layout_constraintHeight_max="300dp"
app:layout_constraintHorizontal_weight="1"
android:layout_margin="1dp"
android:text="x"
android:textColor="@color/greybackgroundd"
style="@style/style13buttontext"
android:onClick="EditorClicksModifiedForEditorLayout"
android:background="@drawable/alternativecalcbuttonyellow"
tools:ignore="MissingConstraints"
/>
以下是 Style 属性的定义方式:
<style name="style13buttontext">
<item name="android:fontFamily">@font/dosis_extrabold</item>
<item name="android:textSize">13sp</item>
<item name="android:textStyle">bold</item>
<item name="android:textAllCaps">false</item>
</style>
另外,请记住,我使用 Flow 助手来组织 ConstraintLayout 中的按钮,该按钮用作我在代码中创建的 Fragment 对象的布局。
因此,此布局在 Fragment 对象 onCreateView 内部膨胀。我一直在尝试修改文本的重力,但它总是水平居中显示在按钮的顶部。
奇怪的行为是,片段中的布局显示在屏幕上,一个聊天气泡从 Facebook 出现在屏幕上,布局自行组织,并且神奇地所有按钮都显示居中的文本。
在 Fragment 对象的 onAttach 方法中,我获得了按钮并更改按钮文本的属性(例如文本颜色)并设置 singleLine = true。所有这些更改都有效,但永远无效的是文本居中。
我努力了:
button.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICALLY); //no working
button.setGravity(Gravity.CENTER); //no working
奇怪的行为是,当我使用 button.setGravity(Gravity.CENTER_VERTICALLY) 时,它会将文本放置在按钮的开始位置和顶部位置。
任何想法或建议
谢谢