【问题标题】:Linearlayout above Relativelayout ( gravity bottom )相对布局上方的线性布局(重力底部)
【发布时间】:2018-11-30 17:10:47
【问题描述】:

布局换行内容不起作用

下面是我的 XML 代码。我想将灰色部分线性布局放置在主图像上方。 Screenshot of my code here.

在宽高比为 16:9 的设备中设计是正确的,但在 18:9 和 18.5:9 中,灰色部分位于顶部。

主题文本和图标必须位于主图的左上角。

The design i want here

别管顶面。从底部开始,高度必须是图像的大小

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:background="@color/light_green"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/dark_green">

        <LinearLayout
            android:id="@+id/topLin"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@color/grey"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/nameTxt"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginStart="10dp"
                android:ellipsize="end"
                android:gravity="center|start"
                android:maxLength="25"
                android:singleLine="true"
                android:text="Abin Stanly"
                android:textColor="@color/white"
                android:textStyle="bold" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:adjustViewBounds="true"
                android:src="@drawable/test" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignTop="@id/imageView"
                android:layout_marginStart="20dp"
                android:layout_marginTop="15dp">

                <ImageView
                    android:id="@+id/icon"
                    android:layout_width="@dimen/icon_height"
                    android:layout_height="@dimen/icon_height"
                    android:layout_gravity="center"
                    android:src="@drawable/ic_love" />

                <TextView
                    android:id="@+id/topicTxt"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:gravity="center"
                    android:paddingEnd="10dp"
                    android:shadowColor="@color/grey"
                    android:shadowDx=".5"
                    android:shadowDy=".5"
                    android:shadowRadius="3.5"
                    android:text="Topic"
                    android:textColor="@color/white"
                    android:textStyle="bold" />


            </LinearLayout>

        </RelativeLayout>

    </LinearLayout>
</LinearLayout>

【问题讨论】:

  • 从线性布局中移除 android:gravity="center"
  • 您需要精简布局。而是使用ContraintLayoutFrameLayoutRelativeLayout。然后你就可以使用android:layout_topOf="@id/parentLinear"developer.android.com/training/constraint-layout
  • @KaranMer 移除所有重力。没用
  • 你的imageview高度是wrapcontent,设置成match_parent。
  • @AbinStanly 您能否提供您需要的确切用户界面?

标签: android android-linearlayout android-relativelayout


【解决方案1】:

这里主布局是RelativeLayout,孩子都是layout_alignParentBottom="true"

您可以随意切换子布局,直到获得所需的布局

提示:使用的布局越少,在慢速设备上的渲染速度就越快。

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/light_green">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/grey">
        <TextView
            android:id="@+id/nameTxt"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginStart="10dp"
            android:ellipsize="end"
            android:gravity="center|start"
            android:maxLength="25"
            android:singleLine="true"
            android:text="Abin Stanly"
            android:textColor="@color/white"
            android:textStyle="bold" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">
        <ImageView
            android:id="@+id/icon"
            android:layout_width="@dimen/icon_height"
            android:layout_height="@dimen/icon_height"
            android:layout_gravity="center"
            android:src="@drawable/ic_love" />
    </LinearLayout>
</RelativeLayout>

【讨论】:

  • 感谢您的提示 :) 。但是答案中的图像视图布局在哪里。
【解决方案2】:

我已将weightSum 添加到父LinearLayout,这样您就可以根据自己的要求轻松划分屏幕了。

已分配View android:layout_weight="3",因此它将覆盖屏幕的 3/10 部分,您可以根据自己的感觉进行更改。

<?xml version="1.0" encoding="utf-8"?>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:background="@android:color/holo_green_light"
        android:orientation="vertical"
        android:weightSum="10"
        xmlns:android="http://schemas.android.com/apk/res/android">


        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="3" 
            android:background="@android:color/holo_green_light"
            />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="7">



            <TextView
                android:id="@+id/nameTxt"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:maxLength="25"
                android:padding="11dp"
                android:background="@android:color/darker_gray"
                android:text="Abin Stanly"
                android:textColor="@android:color/white"
                android:textStyle="bold" />

            <RelativeLayout
                android:id="@+id/RelativeLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/nameTxt"
                android:layout_alignParentBottom="true"

                >

                <ImageView
                    android:id="@+id/imageView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:scaleType="centerCrop"
                    android:src="@drawable/mobile_world" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignTop="@id/imageView"
                    android:background="@android:color/black"
                    android:padding="11dp"
                    >

                    <ImageView
                        android:id="@+id/icon"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:layout_gravity="center"
                        android:src="@drawable/back_arrow" />

                    <TextView
                        android:id="@+id/topicTxt"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="8dp"
                        android:gravity="center"
                        android:paddingEnd="10dp"
                        android:shadowColor="@android:color/darker_gray"
                        android:shadowDx=".5"
                        android:shadowDy=".5"
                        android:shadowRadius="3.5"
                        android:layout_gravity="center_vertical"
                        android:text="Topic"
                        android:textColor="@android:color/white"
                        android:textStyle="bold" />


                </LinearLayout>

            </RelativeLayout>

        </RelativeLayout>
    </LinearLayout>

输出

【讨论】:

  • 不要添加比例类型,因为我想显示原始图像大小。我想像imgur.com/a/UgruxgQ那样在图像上附加图标和文本而不是它上面。别管顶面。从底部高度必须包裹图像的宽度
  • 具有不同分辨率(如更大宽度)图像的设备将在左侧,右侧有间隙
  • 这就是我添加比例类型android:scaleType="centerCrop"的原因。要么你应该使用它或android:scaleType="fitXY"
  • 一些图片包含左端的文字。如果 scaletype 是 centercrop 或 fitxy ,它将剪切
【解决方案3】:

检查这个布局:

<?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"
    android:background="@android:color/holo_green_light">

    <TextView
        android:id="@+id/nameTxt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/imageView"
        android:background="@android:color/darker_gray"
        android:maxLength="25"
        android:padding="10dp"
        android:text="Abin Stanly"
        android:textColor="@android:color/white"
        android:textStyle="bold" />


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:scaleType="centerCrop"
        android:src="@drawable/test" />

    <TextView
        android:id="@+id/topicTxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageView"
        android:layout_gravity="center_vertical"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:drawableStart="@drawable/ic_love"
        android:drawablePadding="10dp"
        android:gravity="center"
        android:paddingEnd="10dp"
        android:shadowColor="@android:color/darker_gray"
        android:shadowDx=".5"
        android:shadowDy=".5"
        android:shadowRadius="3.5"
        android:text="Topic"
        android:textColor="@android:color/white"
        android:textStyle="bold" />
</RelativeLayout>

【讨论】:

  • 一些图片包含左端的文字。如果 scaletype 是 centercrop 或 fitxy ,它将剪切。具有不同分辨率(如更多宽度)的设备图像将在左侧,右侧有间隙
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-06
  • 1970-01-01
相关资源
最近更新 更多