Android 布局中的等距按钮

Android 布局中的等距按钮,android,xml,layout,button,Android,Xml,Layout,Button,我试图在Android上制作4个按钮,在纵向视图中均匀分布 空间应根据屏幕大小进行放大和缩小,每个按钮和边框之间的空间应均匀 我尝试使用线性布局,重量和布局重力,但似乎我不能垂直居中按钮 这是安卓布局的一个缺陷吗?更可能的只是我 <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" xm

我试图在Android上制作4个按钮,在纵向视图中均匀分布

空间应根据屏幕大小进行放大和缩小,每个按钮和边框之间的空间应均匀

我尝试使用线性布局,重量和布局重力,但似乎我不能垂直居中按钮

这是安卓布局的一个缺陷吗?更可能的只是我

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:layout_gravity="center"
>
<Button
android:id="@+id/btn_f"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn_f"
>
</Button>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_weight="1"
android:layout_gravity="center"
>
<Button
android:id="@+id/btn_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn_b"
>
</Button>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:layout_gravity="center"
>
<Button
android:id="@+id/btn_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn_a"
>
</Button>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:layout_gravity="center"
>
<Button
android:id="@+id/settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Settings"
>
</Button>
</LinearLayout>
</LinearLayout>

您似乎将所有按钮包装在另一个
线性布局中,而不仅仅是将它们保存在父
线性布局中

删除所有直接的
LinearLayout
,以便
android:layout\u weight
生效


实现拉伸和间隔

  • 在按钮本身中使用
    android:layout\u width=“fill\u parent”
  • 指定间距的边距

试试这样的方法:

<?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="fill_parent"
    android:orientation="vertical"
    >
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:orientation="vertical"
        android:layout_weight="1"
        android:gravity="center"
        >
        <Button
            android:id="@+id/btn_f"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="btn_f"
            />
    </LinearLayout>
    <!-- etc -->
</LinearLayout>

与原始布局不同的是,每个内部
LinearLayout
的高度为零,重心(不是布局重心)为中心。这将导致所有内部线性布局具有相同的高度,均匀地分割父级高度,而不拉伸按钮本身。

尝试以下方法:

 <LinearLayout         
  android:layout_width="wrap_content"         
  android:layout_height="0dp"         
  android:orientation="horizontal"         
  android:weightSum="1"         
  android:gravity="center">
     <Button             
        android:id="@+id/btn_f"    
        android:layout_width="0dp" 
        android:layout_weight="0.5" 
        android:layout_height="wrap_content"      
        android:text="btn_f" />     
 </LinearLayout> 


这会将按钮设置为仅占屏幕长度的一半。

这只需拉伸按钮即可。。。我希望按钮要小,周围有空间。。。矩形中心的每一个指定边距都是我想要避免的。。。你需要放一个尺寸,即使它是在倾斜的最终效果是不一样的每一个显示器。。。难道没有一种自动的方法,比如体重控制吗?这种方法不起作用。。。但如果你同时指定布局和重心,它就起作用了。。。。可以肯定的是,android xml界面的文档记录很差……它应该是android:layout\u gravity=“center”而不是android:gravity=“center”。这里没有必要使用android:gravity=“center”。重力标签影响其内容的定位,而布局重力影响其自身相对于其父项的定位。这将拉伸按钮。