Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android LinerLayout未对齐_Android_Android Layout - Fatal编程技术网

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"
...