【问题标题】:layout 4 images 50% width and height布局 4 个图像 50% 宽度和高度
【发布时间】:2018-04-08 15:30:24
【问题描述】:

我正在尝试为我的 Android 应用程序创建一个布局,我想在其中显示图像,每个图像的宽度应为 50%,高度为 50%,到目前为止,我如何实现这一点,我构建了以下内容:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="test.com.tests.MainActivity"
    tools:showIn="@layout/app_bar_main">

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:stretchColumns="1">

        <TableRow>
            <!-- Column 1 -->
            <ImageButton
                android:id="@+id/tbl_txt1"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_margin="4dip"
                android:layout_weight="1"
                android:background="@color/red"
                android:padding="10dip"
                android:text="Column 1"
                android:textColor="@color/white" />

            <!-- Column 2 -->
            <ImageButton
                android:id="@+id/tbl_txt2"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_margin="4dip"
                android:layout_weight="1"
                android:background="@color/red"
                android:padding="10dip"
                android:text="Column 2"
                android:textColor="@color/white" />

        </TableRow>
    </TableLayout>

</android.support.constraint.ConstraintLayout>

类似这样的: https://ibb.co/c2Gngc

【问题讨论】:

  • 发布预期的输出图像。
  • @Abhi 像这样我的绘画技巧很烂ibb.co/c2Gngc

标签: android android-layout android-xml


【解决方案1】:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/ConstraintLayout2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            app:layout_constraintBottom_toTopOf="@+id/imageView4"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/imageView2"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@mipmap/ic_launcher" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            app:layout_constraintBottom_toBottomOf="@+id/imageView1"
            app:layout_constraintLeft_toRightOf="@+id/imageView1"
            app:layout_constraintRight_toLeftOf="@+id/imageView3"
            app:layout_constraintTop_toTopOf="@+id/imageView1"
            app:srcCompat="@mipmap/ic_launcher_round" />

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            app:layout_constraintBottom_toBottomOf="@+id/imageView2"
            app:layout_constraintLeft_toRightOf="@+id/imageView2"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="@+id/imageView2"
            app:srcCompat="@mipmap/ic_launcher_round" />

        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/imageView5"
            app:layout_constraintTop_toBottomOf="@+id/imageView1"
            app:srcCompat="@mipmap/ic_launcher_round" />

        <ImageView
            android:id="@+id/imageView5"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/imageView4"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="@+id/imageView4"
            app:srcCompat="@mipmap/ic_launcher_round" />
    </android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>

【讨论】:

    【解决方案2】:

    如果你只想使用 TableLayout,你可以使用类似的东西

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/darker_gray">
    
            <TableRow android:layout_weight="1">
    
            </TableRow>
    
            <TableRow android:layout_weight="1">
    
            </TableRow>
    
        </TableLayout>
    
        <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/darker_gray">
    
            <TableRow android:layout_weight="1">
    
            </TableRow>
    
            <TableRow android:layout_weight="1">
    
            </TableRow>
    
        </TableLayout>
    
    </LinearLayout>
    

    或者类似的东西

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1">
    
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1">
    
            </LinearLayout>
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1">
    
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1">
    
            </LinearLayout>
    
        </LinearLayout>
    
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      试试这个

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          android:weightSum="4">
      
          <RelativeLayout
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_weight="2">
      
              <LinearLayout
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_centerInParent="true"
                  android:weightSum="2">
      
                  <ImageView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_weight="1"
                      android:src="@drawable/image" />
      
                  <ImageView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_weight="1"
                      android:src="@drawable/image" />
              </LinearLayout>
      
          </RelativeLayout>
      
          <RelativeLayout
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_weight="2">
      
              <LinearLayout
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_centerInParent="true"
                  android:weightSum="2">
      
                  <ImageView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_weight="1"
                      android:src="@drawable/image" />
      
                  <ImageView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_weight="1"
                      android:src="@drawable/image" />
              </LinearLayout>
      
          </RelativeLayout>
      
      </LinearLayout>
      

      【讨论】:

      • 我想稍后动态地将数据添加到该视图中,我是否总是需要先生成一个线性的相对布局,然后是图像视图?
      • 你需要更清楚你的帖子标题,你提到你需要4张图片,50%
      • 如果只有 4 个项目,您可以使用它来动态填充项目,否则尝试 recyclerview/gridview
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多