【问题标题】:centering layout android same height居中布局android相同高度
【发布时间】:2016-08-30 02:47:26
【问题描述】:

我无法通过 imgur 上传图片,所以我将通过驱动器发布链接:

https://drive.google.com/file/d/0B9Ho1o8QjLVaLWsyNmxnQTV1alU/view?usp=sharing

这是 xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <LinearLayout
            android:id="@+id/lay"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/imgView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@mipmap/ic_launcher"
                android:layout_weight="1"/>

            <ImageView
                android:id="@+id/imgView2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@mipmap/ic_launcher"
                android:layout_weight="1"/>
        </LinearLayout>
        <LinearLayout
            android:id="@+id/lay2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/lay"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/imgView3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@mipmap/ic_launcher"
                android:layout_weight="1"/>

            <ImageView
                android:id="@+id/imgView4"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@mipmap/ic_launcher"
                android:layout_weight="1"/>
        </LinearLayout>

    </LinearLayout>

    <Button
        android:id="@+id/buttonLoadPicture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Load Picture"
        android:layout_centerInParent="true"
        android:layout_alignParentBottom="true"/>
</RelativeLayout>

我需要做的是布局中心,像这样:

https://drive.google.com/file/d/0B9Ho1o8QjLVaWDVjWk02ckVWeEU/view?usp=sharing

请帮忙,提前谢谢!

【问题讨论】:

    标签: android xml layout


    【解决方案1】:

    您必须将weightSum 属性添加到LinearLayout 并为android:layout_width 设置0dp 以获得图像视图,如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:weightSum="2">
            <LinearLayout
                android:id="@+id/lay"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:weightSum="2"
                android:orientation="horizontal">
    
                <ImageView
                    android:id="@+id/imgView"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:src="@mipmap/ic_launcher"
                    android:layout_weight="1"/>
    
                <ImageView
                    android:id="@+id/imgView2"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:src="@mipmap/ic_launcher"
                    android:layout_weight="1"/>
            </LinearLayout>
            <LinearLayout
                android:id="@+id/lay2"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_below="@+id/lay"
                android:orientation="horizontal"
                android:layout_weight="1"
                android:weightSum="2">
    
                <ImageView
                    android:id="@+id/imgView3"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:src="@mipmap/ic_launcher"
                    android:layout_weight="1"/>
    
                <ImageView
                    android:id="@+id/imgView4"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:src="@mipmap/ic_launcher"
                    android:layout_weight="1"/>
            </LinearLayout>
    
        </LinearLayout>
    
        <Button
            android:id="@+id/buttonLoadPicture"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Load Picture"
            android:layout_centerInParent="true"
            android:layout_alignParentBottom="true"/>
    </RelativeLayout>
    

    【讨论】:

      【解决方案2】:

      您也应该在垂直布局中使用weight。

      试试这个代码。

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">
              <LinearLayout
                  android:id="@+id/lay"
                  android:layout_width="match_parent"
                  android:layout_height="0dp"
                  android:layout_weight="1"
                  android:orientation="horizontal">
      
                  <ImageView
                      android:id="@+id/imgView"
                      android:layout_width="0dp"
                      android:layout_height="match_parent"
                      android:src="@mipmap/ic_launcher"
                      android:layout_weight="1"/>
      
                  <ImageView
                      android:id="@+id/imgView2"
                      android:layout_width="0dp"
                      android:layout_height="match_parent"
                      android:src="@mipmap/ic_launcher"
                      android:layout_weight="1"/>
              </LinearLayout>
              <LinearLayout
                  android:id="@+id/lay2"
                  android:layout_width="match_parent"
                  android:layout_height="0dp"
                  android:layout_below="@+id/lay"
                  android:orientation="horizontal"
                  android:layout_weight="1">
      
                  <ImageView
                      android:id="@+id/imgView3"
                      android:layout_width="0dp"
                      android:layout_height="match_parent"
                      android:src="@mipmap/ic_launcher"
                      android:layout_weight="1"/>
      
                  <ImageView
                      android:id="@+id/imgView4"
                      android:layout_width="0dp"
                      android:layout_height="match_parent"
                      android:src="@mipmap/ic_launcher"
                      android:layout_weight="1"/>
              </LinearLayout>
      
          </LinearLayout>
      
          <Button
              android:id="@+id/buttonLoadPicture"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Load Picture"
              android:layout_centerInParent="true"
              android:layout_alignParentBottom="true"/>
      </RelativeLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多