我有2个按钮和一个水平线性布局内的TextView。所有三个视图都有权重。我一直没有成功将两个按钮垂直居中。不能垂直居中按钮
我曾尝试:
android:layout_gravity="center"
android:gravity="center"
线性布局
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#707070"
>
<Button
android:id="@+id/ratioLBButton"
android:text="LB"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:onClick="ratioLBFunction"
/>
<Button
android:id="@+id/ratioKGButton"
android:text="KG"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:onClick="ratioKGFunction"
/>
<TextView
android:text="Hello World"
android:textSize="18sp"
android:gravity="center"
android:id="@+id/ratioOutput"
android:background="#707070"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="5"
/>
</LinearLayout>
这是包括上述意见父布局。
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:background="#989898"
>
这是我的平板电脑的屏幕截图。这是我的android手机完全相同。
你的代码显示的是彼此相邻的按钮,你是否试图让它们在彼此之上?同时保持水平条? – nbroeking
或让它们居中水平布局 – nbroeking
水平相邻,但垂直居中在线性布局 – th3ramr0d