Android LinerLayout未对齐
为什么Android LinerLayout未对齐,android,android-layout,Android,Android Layout,为什么TextView和按钮不对齐?它们应该是相同的高度,并且没有重力作用。我试着用重力固定,但没有成功。是虫子吗 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap
TextView
和按钮不对齐?它们应该是相同的高度,并且没有重力作用。我试着用重力固定,但没有成功。是虫子吗
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tv1"
android:layout_width="70sp"
android:layout_height="wrap_content"
android:text="My test test test test test test test test test test"
android:textSize="10sp" />
<Button
android:id="@+id/btn1"
android:layout_width="90sp"
android:layout_height="wrap_content"
android:text="My Button 1 Button Button"
android:textSize="10sp" />
<Button
android:id="@+id/btn2"
android:layout_width="90sp"
android:layout_height="wrap_content"
android:text="My Button 2 Button Button"
android:textSize="10sp" />
<Button
android:id="@+id/btn3"
android:layout_width="70sp"
android:layout_height="wrap_content"
android:text="Info"
android:textSize="10sp" />
</LinearLayout>
是的,这是线性布局的缺点……你不需要;不要有太多的选项…而是使用相对布局或…设置“android:baselineAligned=“false”作为线性布局属性或设置按钮的顶部边距…就像我所做的那样
像
尝试将线性布局重心设置为垂直中心:
...
android:gravity="center_vertical"
...
希望有帮助。这是由于线性布局基线对齐,这里您有完整的解释使用dp表示视图,使用sp表示文本。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tv1"
android:layout_width="70sp"
android:layout_height="60dp"
android:text="My test test test test test test test test test test"
android:textSize="10sp"/>
<Button
android:id="@+id/btn1"
android:layout_width="90sp"
android:layout_height="60dp"
android:text="My Button 1 Button Button"
android:textSize="10sp"
android:layout_marginTop="16dp" />
<Button
android:id="@+id/btn2"
android:layout_width="90sp"
android:layout_height="60dp"
android:text="My Button 2 Button Button"
android:textSize="10sp"
android:layout_marginTop="16dp" />
<Button
android:id="@+id/btn3"
android:layout_width="70sp"
android:layout_height="60dp"
android:text="Info"
android:textSize="10sp"
android:layout_marginTop="22dp" />
</LinearLayout>
...
android:gravity="center_vertical"
...