Android 当重心不工作时,如何在按钮中对齐文本
我有以下资料:Android 当重心不工作时,如何在按钮中对齐文本,android,android-layout,textview,Android,Android Layout,Textview,我有以下资料: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:layout_width
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:gravity="left|center_vertical" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
android:gravity="left|center_vertical" />
</LinearLayout
而不是包装内容,给一些价值。实际上它可以工作,但你看不见,因为你的宽度被设置为包裹内容
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Button"
android:gravity="left|center_vertical" />
设置对象的重力属性时,您告诉该对象您希望其内容对齐的位置。在您的代码中,您试图将文本对齐到一个只与文本本身一样大的框中,而该框不起任何作用
有两种方法可以解决这个问题
A) 您可以将按钮的宽度设置为不包裹内容
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Button"
android:gravity="left|center_vertical" />
B) 您可以将文本视图的宽度设置为不换行内容
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Text"
android:gravity="left|center_vertical" />
否则,您现在正在执行的操作应该在按钮中包含一些文本,在文本边缘和按钮外部之间没有空格。如果有空间,请确保没有在按钮或文本中设置填充或边距。对于“换行”内容,我不确定这个想法是否有意义-按钮的大小与需要的大小完全相同,文本不能再往前走了。是的,但是文本和按钮左边缘之间有空格。是的,但是文本和按钮左边缘之间有空格。你说的是多少空格?如果它类似于1或2 dp,则它是按钮的默认属性。为此,您需要创建自定义按钮。