开发者

Android relative layout gravity fill problem

开发者 https://www.devze.com 2023-03-29 05:24 出处:网络
I am trying to create a layout of 8 image-buttons that fill the screen. I have the following code: <RelativeLayout

I am trying to create a layout of 8 image-buttons that fill the screen.

I have the following code:

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:clickable="true"
  android:gravity="fill"
  android:background="@drawable/background">

  <LinearLayout android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:scaleType="fitXY">

    <ImageButton android:src="@drawable/calls_r1_c1"
                 android:id="@+id/basic"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:scaleType="fitXY"
                 android:background="@android:color/transparent"/>

    <ImageButton android:src="@drawable/calls_r2_c1"
                 android:id="@+id/basic"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:scaleType="fitXY"
                 android:background="@android:color/transparent"/>

    <ImageButton android:src="@drawable/calls_r3_c1"
                 android:id="@+id/basic"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:scaleType="fitXY"
                 android:background="@android:color/transparent"/>

    <ImageButton android:src="@drawable/calls_r4_c1"
                 android:id="@+id/basic"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:scaleType="fitXY"
                 android:background="@android:color/transparent"/>

    <ImageButton android:src="@drawable/calls_r5_c1"
                 android:id="@+id/basic"
                 android:layout_width="fill_parent"
              开发者_C百科   android:layout_height="wrap_content"
                 android:scaleType="fitXY"
                 android:background="@android:color/transparent"/>

    <ImageButton android:src="@drawable/calls_r6_c1"
                 android:id="@+id/basic"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:scaleType="fitXY"
                 android:background="@android:color/transparent"/>

    <ImageButton android:src="@drawable/calls_r7_c1"
                 android:id="@+id/basic"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:scaleType="fitXY"
                 android:background="@android:color/transparent"/>

    <ImageButton android:src="@drawable/calls_r8_c1"
                 android:id="@+id/basic"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:scaleType="fitXY"
                 android:background="@android:color/transparent"/>
   </LinearLayout>  
</RelativeLayout>

The width of the image buttons are filling the parent, but the height is not.

How do I make them so that, their height varies to fit the screen?

Is this to be done in java?

If so, what is the most efficient way?

Thank you.


try providing android:layout_weight="1" to each ImageButton.


Isn't it better to do in the next way?

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:clickable="true"
  android:background="@drawable/background">

    <ImageButton android:src="@drawable/calls_r1_c1"
                 android:id="@+id/basic"
                 android:layout_width="fill_parent"
                 android:layout_height="0dp"
                 android:layout_weight="10"
                 android:background="@android:color/transparent"/>

    <ImageButton android:src="@drawable/calls_r2_c1"
                 android:id="@+id/basic"
                 android:layout_width="fill_parent"
                 android:layout_height="0dp"
                 android:layout_weight="10"
                 android:background="@android:color/transparent"/>

    <ImageButton android:src="@drawable/calls_r3_c1"
                 android:id="@+id/basic"
                 android:layout_width="fill_parent"
                 android:layout_height="0dp"
                 android:layout_weight="10"
                 android:background="@android:color/transparent"/>

    <ImageButton android:src="@drawable/calls_r4_c1"
                 android:id="@+id/basic"
                 android:layout_width="fill_parent"
                 android:layout_height="0dp"
                 android:layout_weight="10"
                 android:background="@android:color/transparent"/>

    <ImageButton android:src="@drawable/calls_r5_c1"
                 android:id="@+id/basic"
                 android:layout_width="fill_parent"
                 android:layout_height="0dp"
                 android:layout_weight="10"
                 android:background="@android:color/transparent"/>

    <ImageButton android:src="@drawable/calls_r6_c1"
                 android:id="@+id/basic"
                 android:layout_width="fill_parent"
                 android:layout_height="0dp"
                 android:layout_weight="10"
                 android:background="@android:color/transparent"/>

    <ImageButton android:src="@drawable/calls_r7_c1"
                 android:id="@+id/basic"
                 android:layout_width="fill_parent"
                 android:layout_height="0dp"
                 android:layout_weight="10"
                 android:background="@android:color/transparent"/>

    <ImageButton android:src="@drawable/calls_r8_c1"
                 android:id="@+id/basic"
                 android:layout_width="fill_parent"
                 android:layout_height="0dp"
                 android:layout_weight="10"
                 android:background="@android:color/transparent"/>
   </LinearLayout>

Instead of using RelativeLayout and gravity configuration?


Try it this way:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:clickable="true"
    android:gravity="fill" android:background="#FFFFFF">

    <LinearLayout android:orientation="vertical"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:scaleType="fitXY">

        <ImageButton android:src="@drawable/a" android:id="@+id/basic"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:scaleType="fitXY" android:background="@android:color/transparent"
            android:layout_weight="1" />

        <ImageButton android:src="@drawable/b" android:id="@+id/basic"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:scaleType="fitXY" android:background="@android:color/transparent"
            android:layout_weight="1" />

        <ImageButton android:src="@drawable/c" android:id="@+id/basic"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:scaleType="fitXY" android:background="@android:color/transparent"
            android:layout_weight="1" />

        <ImageButton android:src="@drawable/d" android:id="@+id/basic"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:scaleType="fitXY" android:background="@android:color/transparent"
            android:layout_weight="1" />

        <ImageButton android:src="@drawable/e" android:id="@+id/basic"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:scaleType="fitXY" android:background="@android:color/transparent"
            android:layout_weight="1" />

        <ImageButton android:src="@drawable/f" android:id="@+id/basic"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:scaleType="fitXY" android:background="@android:color/transparent"
            android:layout_weight="1" />

        <ImageButton android:src="@drawable/g" android:id="@+id/basic"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:scaleType="fitXY" android:background="@android:color/transparent"
            android:layout_weight="1" />

        <ImageButton android:src="@drawable/h" android:id="@+id/basic"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:scaleType="fitXY" android:background="@android:color/transparent"
            android:layout_weight="1" />
    </LinearLayout>
</RelativeLayout>

0

精彩评论

暂无评论...
验证码 换一张
取 消