【问题标题】:Linear Layout using weight to adapt screen size使用权重来适应屏幕尺寸的线性布局
【发布时间】:2017-06-14 20:02:43
【问题描述】:

我正在尝试将线性布局用作我的 recyclerView/adapter 的一行,基本上我的行有 3 个元素:一个图像、一个 textView 和一个带有一些文本的 checkButton。

我想要的是图像占据 50% 的宽度加上 40 的高度,所以我将图像的权重设置为 2。

接下来我需要另一个 50% 的布局,其中包含其他 2 个元素,我希望在图像的右上角是 textView,在底部是复选框。

我之前已经用相对布局实现了这个,但是内容不适应不同的设备,所以我尝试了线性布局和权重属性。

这就是我目前所拥有的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
    android:orientation="horizontal">

        <ImageView
            android:id="@+id/plantPhoto"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:contentDescription="photo " />


        <com.example.afcosta.inesctec.pt.android.Helpers.NexusBoldTextView
           android:id="@+id/plantName"
           android:layout_width="0dp"
           android:layout_height="wrap_content"
           android:layout_weight="2"
           android:textColor="@color/nephritis"
           android:textSize="15sp" />

        <CheckBox
            android:id="@+id/plantCheck"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignStart="@+id/plantName"
            android:layout_below="@+id/plantName"
            android:layout_marginTop="25dp"
            android:button="@drawable/customdrawablecheckbox"
            android:textSize="10dp" />


</LinearLayout>

这是我目前拥有的图像:image

正如我所解释的,我需要图像占据 50% 的宽度,并且绿色文本与右侧的照片并排。

复选框必须在下方,有帮助吗??

谢谢

【问题讨论】:

    标签: java android xml android-layout android-linearlayout


    【解决方案1】:

    试试这个,

    <?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="wrap_content"
        android:orientation="horizontal">
    
        <ImageView
            android:id="@+id/plantPhoto"
            android:layout_width="fill_parent"
            android:layout_height="70dp"
            android:layout_weight="0.5"
            android:contentDescription="photo "
            android:src="@drawable/placeholder" />
    
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.5">
    
            <TextView
                android:id="@+id/plantName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:textColor="@color/colorBlack"
                android:textSize="15sp" />
    
            <CheckBox
                android:id="@+id/plantCheck"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/plantName"
                android:layout_marginTop="5dp"
                android:text="CheckBox"
                android:textSize="15dp" />
        </RelativeLayout>
    </LinearLayout>
    

    【讨论】:

    • 很好的解决方案,正是我需要的!现在我明白它是如何工作的了,谢谢!
    【解决方案2】:

    对我来说,它必须设置为相对布局。然后,要让元素“适应不同的设备”,您需要将 android:layout_width 和 height 设置为 wrap_content 或 fill_parent ,具体取决于您想要实现的目标。

    【讨论】:

    • 但是怎么做呢?例如对于我的 imageView 我如何指定宽度?我可以为高度包装内容,但宽度呢?
    • 啊,我明白了。您必须使用线性布局 wieght attrbiute,您可以在其中添加另一个相对布局以将元素置于另一个之上。
    【解决方案3】:

    将图像权重设置为 50%,即 0.5,您在该布局上添加的任何内容都将是水平的,因此要解决此问题,只需添加另一个权重为 0.5 的布局并将其设置为垂直。

    【讨论】:

    • 您能分享一个解决方案示例吗?
    • 您能否添加您想要实现的目标的屏幕截图或图片,而不仅仅是您所做的事情的图片。
    【解决方案4】:

    你可以试试这样的

        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout 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="wrap_content"
            android:orientation="horizontal">
    
            <ImageView
                android:id="@+id/plantPhoto"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:contentDescription="photo " />
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">
    
                <com.example.afcosta.inesctec.pt.android.Helpers.NexusBoldTextView
                    android:id="@+id/plantName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/nephritis"
                    android:textSize="15sp" />
    
                <CheckBox
                    android:id="@+id/plantCheck"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:layout_marginTop="25dp"
                    android:button="@drawable/customdrawablecheckbox"
                    android:textSize="10dp" />
            </LinearLayout>
        </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-14
      • 1970-01-01
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-09
      相关资源
      最近更新 更多