如何用视图和布局填充Android屏幕?

时间:2013-08-22 06:34:45

标签: android android-layout-weight

Problem Image

我有一个包含以下视图和嵌套相对布局的相对布局。对于某些设备,屏幕底部留有空白区域。我想公平地将空间分配给嵌套视图和viewgroups()来填充屏幕。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/tdmubiobgd"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MasterActivity$DummySectionFragment" >

<Button
    android:id="@+id/ScanButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:text="Scan" />

<ImageView
    android:id="@+id/scan_adsView"
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:layout_alignLeft="@id/ScanButton"
    android:layout_alignParentTop="true"
    android:scaleType="fitXY"
    android:src="@drawable/ads" />

<RelativeLayout
    android:id="@+id/scan_header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/scan_adsView"
    android:layout_below="@id/scan_adsView"
    android:layout_marginTop="10px"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/Scan_Image"
        android:layout_width="72px"
        android:layout_height="72px"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/Scan_Name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/Scan_Image"
        android:text="Name"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/Scan_DName"
        android:layout_width="80dip"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView3"
        android:layout_alignParentRight="true"
        android:layout_gravity="right"
        android:gravity="right"
        android:text="DName "
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/Scan_Sex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/Scan_Name"
        android:layout_toRightOf="@+id/Scan_Age"
        android:text="Male"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/Scan_Age"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/Scan_Sex"
        android:layout_alignBottom="@+id/Scan_Sex"
        android:layout_alignLeft="@+id/Scan_Name"
        android:text="30"
        android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>

<ImageView
    android:id="@+id/scan_level1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/scan_header"
    android:layout_below="@+id/scan_header"
    android:scaleType="fitXY"
    android:src="@drawable/l1warning" />

<RelativeLayout
    android:id="@+id/scan_master"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/scan_level"
    android:layout_below="@id/scan_level1"
    android:layout_marginTop="10px"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/scan_mname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="MinionRush"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/scan_mdetails"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/scan_mname"
        android:layout_below="@+id/scan_mname"
        android:text="DoodlePlay"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/scan_period"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/scan_mdetails"
        android:text="DartRace"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <ImageView
        android:id="@+id/scan_alarm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="20dp"
        android:src="@drawable/alarm" />
</RelativeLayout>

<ImageView
    android:id="@+id/scan_level2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@id/scan_master"
    android:layout_below="@id/scan_master"
    android:scaleType="fitXY"
    android:layout_marginTop="10px"
    android:src="@drawable/toempty" />

<Gallery
    android:id="@+id/scan_gallery"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/scan_level2"
    android:layout_marginTop="10px"
    android:layout_alignParentLeft="true" />

    <ImageView
        android:id="@+id/faq"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/scan_gallery"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10px"
        android:scaleType="fitXY"
        android:src="@drawable/faq" />

</RelativeLayout>

5 个答案:

答案 0 :(得分:2)

使用以下内容:

<?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" 
    android:background="#ffffff">

    <ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:src="@drawable/ic_launcher" />

    <RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:layout_weight="1"
    android:background="#556677">
    </RelativeLayout>

    <Gallery
    android:id="@+id/gallery1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:background="#664411"/>

    <RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:layout_weight="1"
    android:background="#997766">
    </RelativeLayout>
</LinearLayout>

希望这会对你有帮助....

答案 1 :(得分:1)

使用线性布局作为父布局,并为所有布局赋予权重。

答案 2 :(得分:0)

以下是您的问题的XML

  <?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:src="@drawable/ic_launcher" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
     android:layout_weight="1">
</RelativeLayout>

<Gallery
    android:id="@+id/gallery1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
     android:layout_weight="1" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
     android:layout_weight="1">
</RelativeLayout>

答案 3 :(得分:0)

使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout     
android:layout_width="fill_parent"
android:layout_height="fill_parent"
orientation="vertical">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="0dp" 
android:layout_weight="1"
android:src="@drawable/ic_launcher" />

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp" 
android:layout_weight="1">
</RelativeLayout>

<Gallery
android:id="@+id/gallery1"
android:layout_width="match_parent"
android:layout_height="0dp" 
android:layout_weight="1"
/>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp" 
android:layout_weight="1">
</RelativeLayout>

</LinearLayout>

答案 4 :(得分:-1)

使用线性布局并添加  android:layout:weight="1" 在每个标签.. 如果你想使用相对布局那么,如果你有四个视图,最好你应该获得屏幕尺寸并将其除以四个

请参阅 this link 获取屏幕尺寸