【问题标题】:Android - Vertical and Horizontal WeightSumAndroid - 垂直和水平权重和
【发布时间】:2017-04-20 09:04:01
【问题描述】:

我想在屏幕上放置 4 张图像,分成四等份,并在每张图片的顶部放置一些文字。

我知道如何在水平或垂直方向上使用 weightsum,而不是两者。

WeightSum

<LinearLayout 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"
tools:context="com.example.tiagosilva.amob_android.ProductsFragment"
android:background="@color/AMOB_gray"
android:weightSum="2"
android:orientation="vertical"
android:padding="15dp">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:id="@+id/img_tube_bending"
    android:src="@drawable/fully_electric"
    android:layout_marginRight="5dp"/>

<ImageView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:id="@+id/img_section_bending_rolls"
    android:src="@drawable/section_bending_rolls"
    android:layout_marginLeft="5dp"/>

【问题讨论】:

    标签: android layout


    【解决方案1】:

    与使用 LinearLayout 相比,使用 GridLayout 更容易完成您想要的操作。下面是一个简单的例子,说明如何以象限的方式放置 4 个图像:

    <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/GridLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnCount="2"
    android:rowCount="2"
    android:orientation="horizontal"
    tools:context=".GridXMLActivity">
    <ImageView
        android:id="@+id/image1"
        android:layout_width="0dp"
        android:src="@drawable/Sun3"
        android:layout_columnWeight="1"
        android:adjustViewBounds="true"
        />
    
    <ImageView
        android:id="@+id/image2"
        android:layout_width="0dp"
        android:src="@drawable/Sun3"
        android:layout_columnWeight="1"
        android:adjustViewBounds="true"
        />
    
    <ImageView
        android:id="@+id/image3"
        android:layout_width="0dp"
        android:src="@drawable/Sun3"
        android:layout_columnWeight="1"
        android:adjustViewBounds="true"/>
    
    <ImageView
        android:id="@+id/image4"
        android:layout_width="0dp"
        android:src="@drawable/Sun3"
        android:layout_columnWeight="1"
        android:adjustViewBounds="true"/>
    </GridLayout>
    

    希望对你有帮助。

    【讨论】:

    • 它可以工作,但是当我插入我的图像时,它太大了,以至于它从屏幕上消失了。知道如何解决吗?
    【解决方案2】:
    WeightSum work with ParentLayout like..
    <LinearLayout
    height="match_parent"
    width="match_parent"
    orientation="vertical">
    //if orientation is Landescape weight of child will work horizontally like
    <ImageView`enter code here`
    width="match_parent"
    height="0dp"
    layout_weight=".25"  //this layout will tahe 25% of the parent layout in height
    </LinearLayout>
    

    【讨论】:

    • 此代码不起作用。它只是垂直显示所有图像,而不是水平显示所有图像
    • for Horizo​​ntal //如果方向是 Landescape 孩子的权重将像 enter code hereheight="match_parent 一样水平工作" width="0dp" layout_weight=".25" //此布局将占父布局高度的 25%
    【解决方案3】:

    我创建了一个类似于您期望的 xml 布局。在每个ImageView 上加上TextView

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="5dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:orientation="vertical"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content">
            <TextView
                android:text="Text 1"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
            <ImageView
                android:layout_gravity="center"
                android:src="@drawable/ic_launcher"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
        <LinearLayout
            android:orientation="vertical"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content">
            <TextView
                android:text="Text 2"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
            <ImageView
                android:layout_gravity="center"
                android:src="@drawable/ic_launcher"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <LinearLayout
                android:orientation="vertical"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content">
                <TextView
                    android:text="Text 3"
                    android:gravity="center"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
                <ImageView
                    android:layout_gravity="center"
                    android:src="@drawable/ic_launcher"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>
            <LinearLayout
                android:orientation="vertical"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content">
                <TextView
                    android:text="Text 4"
                    android:gravity="center"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
                <ImageView
                    android:layout_gravity="center"
                    android:src="@drawable/ic_launcher"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 2014-12-05
      • 2016-10-26
      • 1970-01-01
      • 1970-01-01
      • 2013-12-10
      • 1970-01-01
      • 2012-05-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多