在android相对布局中平均划分和定位自定义布局

时间:2018-02-22 07:56:01

标签: android android-layout

这是我previous question的延续。布局更改如下:

npm -v

在root <?xml version="1.0" encoding="utf-8"?> <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="match_parent" android:background="@color/black_70_opacity" android:weightSum="100" tools:context="com.dell.eventapp.ui.activity.HomeActivity"> <LinearLayout android:id="@+id/top_image" android:layout_width="match_parent" android:layout_height="@dimen/img_height" android:background="@drawable/activity_background" android:contentDescription="@string/top_image" android:orientation="horizontal" android:scaleType="centerCrop" android:weightSum="25"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:gravity="center" android:paddingBottom="@dimen/margin_8dp" android:text="@string/cio_summit_monaco_2015" android:textColor="@android:color/white" android:textSize="@dimen/large_font" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/bottom_image" android:layout_below="@id/top_image" android:orientation="vertical" android:weightSum="65"> <LinearLayout android:layout_width="match_parent" android:layout_height="160sp" android:layout_marginStart="@dimen/margin_20dp" android:layout_marginTop="@dimen/margin_30dp" android:orientation="horizontal" android:weightSum="2"> <TextView android:id="@+id/agenda" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginRight="@dimen/margin_20dp" android:layout_weight="1" android:background="@drawable/activity_background" android:gravity="center|bottom" android:paddingBottom="@dimen/margin_10dp" android:text="@string/agenda" android:textColor="@android:color/white" android:textSize="@dimen/medium_font" /> <TextView android:id="@+id/sessions" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginRight="@dimen/margin_20dp" android:layout_weight="1" android:background="@drawable/activity_background" android:gravity="center|bottom" android:paddingBottom="@dimen/margin_10dp" android:text="@string/sessions" android:textColor="@android:color/white" android:textSize="@dimen/medium_font" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="160sp" android:layout_marginStart="@dimen/margin_20dp" android:layout_marginTop="@dimen/margin_30dp" android:orientation="horizontal" android:weightSum="2"> <TextView android:id="@+id/speaker" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="bottom" android:layout_marginRight="@dimen/margin_20dp" android:layout_weight="1" android:background="@drawable/activity_background" android:gravity="center|bottom" android:paddingBottom="@dimen/margin_10dp" android:text="@string/speakerbio" android:textColor="@android:color/white" android:textSize="@dimen/medium_font" /> <TextView android:id="@+id/venues" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginRight="@dimen/margin_20dp" android:layout_weight="1" android:background="@drawable/activity_background" android:gravity="center|bottom" android:layout_gravity="center" android:paddingBottom="@dimen/margin_10dp" android:text="@string/venues" android:textColor="@android:color/white" android:textSize="@dimen/medium_font" /> </LinearLayout> </LinearLayout> <RelativeLayout android:id="@+id/bottom_image" android:layout_width="match_parent" android:layout_height="@dimen/img_height1" android:layout_alignParentBottom="true" android:background="@drawable/activity_background" android:contentDescription="@string/top_image" android:scaleType="centerCrop" android:weightSum="10"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:gravity="center" android:paddingBottom="@dimen/margin_8dp" android:text="@string/home_content" android:textColor="@android:color/white" android:textSize="@dimen/large_font" /> </RelativeLayout> </RelativeLayout> 中,我使用了Relativelayout,在android:weightSum="100"页面中,我使用了Linearlayout。在第二个android:weightSum="25"中,我使用了LinearLayout。在嵌套android:weightSum="65"中,我使用了LinearLayout。在第3 android:weightSum="2"条中,我使用了LinearLayout。在android:weightSum="2"的底部,我使用了RelativeLayout。产生的UI如下所示:
UI
使用的自定义布局在顶部和底部不均匀分布。如何使用android:weightSum="10"android:weightsum来实现这一目标?

1 个答案:

答案 0 :(得分:2)

请看一下。我希望它对你有用。

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

    <ImageView
        android:id="@+id/imageview_1"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_alignParentTop="true"
        android:background="#33000000"
        android:src="@android:drawable/ic_menu_gallery" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageview_1"
        android:gravity="center"
        android:text="CIO Summit Monaco 2015"
        android:textColor="#000000"
        android:textSize="18sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/imageview_2"
        android:layout_below="@+id/imageview_1"
        android:gravity="center"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/imageview_11"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:background="#33000000"
                android:src="@android:drawable/ic_menu_gallery" />


            <ImageView
                android:id="@+id/imageview_12"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:background="#33000000"
                android:src="@android:drawable/ic_menu_gallery" />


        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/imageview_21"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:background="#33000000"
                android:src="@android:drawable/ic_menu_gallery" />


            <ImageView
                android:id="@+id/imageview_22"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:background="#33000000"
                android:src="@android:drawable/ic_menu_gallery" />


        </LinearLayout>
    </LinearLayout>

    <ImageView
        android:id="@+id/imageview_2"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true"
        android:background="#33000000"
        android:src="@android:drawable/ic_menu_gallery" />
</RelativeLayout>