Android自适应图像按钮布局

问题描述:

我正在尝试创建以下所示的设计,但是在不同屏幕的图像按钮上具有响应高度. 虽然可以使用android:layout_weight获得响应宽度,但是我无法对按钮的高度应用相同的方法.我想要实现的是下面显示的内容,但是屏幕的高度均匀地分布在各个按钮之间.即每行按钮的屏幕高度的25%. 任何帮助将不胜感激!

I'm trying to create the design shown below but with a responsive height on the image buttons for different screens. While I can get a responsive width by using android:layout_weightI can't manage to apply the same method to the button's height. What I'm trying to achieve is what's shown below but with the height of the screen evenly distributed amongst the buttons. i.e. 25% of the screen's height for each row of buttons. Any help would be greatly appreciated!

  <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
   android:layout_height="fill_parent" >

  <LinearLayout
    android:id="@+id/linear1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="0dp"
        android:layout_weight="0.40"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_pages" />

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="0dp"
        android:layout_weight="0.60"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_pages" />

</LinearLayout>
  <LinearLayout
    android:id="@+id/linear2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/linear1"
    android:layout_alignParentLeft="true"       
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/imageButton3"
        android:layout_width="0dp"
        android:layout_weight="0.60"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_pages" />

</LinearLayout>
  <LinearLayout
    android:id="@+id/linear3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/linear2"
    android:layout_alignParentLeft="true"       
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/imageButton5"
        android:layout_width="0dp"
        android:layout_weight="0.40"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_pages" />

    <ImageButton
        android:id="@+id/imageButton6"
        android:layout_width="0dp"
        android:layout_weight="0.60"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_pages" />

    </LinearLayout>

   <LinearLayout
   android:id="@+id/linear4"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:layout_alignParentLeft="true"
   android:layout_below="@+id/linear3"
   android:orientation="horizontal" >

   <ImageButton
       android:id="@+id/imageButton7"
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:layout_weight="0.60"
       android:src="@drawable/ic_pages" />

   <ImageButton
       android:id="@+id/imageButton8"
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:layout_weight="0.40"
       android:src="@drawable/ic_pages" />
  </LinearLayout>

</RelativeLayout>

在FrameLayout中包装每个水平LinearLayout都达到了预期的效果.

Wrapping each horizontal LinearLayout in a FrameLayout achieved the desired effect.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:orientation="vertical" >
<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp"
    android:layout_weight="0.25">

<LinearLayout
    android:id="@+id/linear1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="0dp"
        android:layout_weight="0.40"
        android:layout_height="fill_parent"
        android:src="@drawable/ic_pages" />

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="0dp"
        android:layout_weight="0.60"
        android:layout_height="fill_parent"
        android:src="@drawable/ic_pages" />

</LinearLayout>
</FrameLayout>

</LinearLayout>

</RelativeLayout>