【问题标题】:android LinearLayout display issueandroid LinearLayout 显示问题
【发布时间】:2016-06-15 06:25:55
【问题描述】:

[问题已解决] 我不知道为什么 height: wrap_content 没有按预期工作,我只是将其更改为匹配父项。总之,问题解决了。不想再在这件奇怪的事情上花更多的时间了。

我正在使用 LinearLayout 来构造 CardView。但是,我遇到了这种奇怪的情况。 Jinesh Francis 的代码运行良好,但是当我将配置复制并粘贴到我的代码中时,问题仍然存在。

为什么长标题不能完全显示?

布局如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.v7.widget.CardView
    android:id="@+id/movieCardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:layout_gravity="center"
    android:background="@color/bacgroundCardView">

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

        <ImageView
            android:id="@+id/movieImage"
            android:layout_width="70dp"
            android:layout_height="90dp"
            android:layout_marginBottom="4dp"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:layout_marginTop="4dp"
            android:scaleType="fitXY"
            android:src="@drawable/selfie" />

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:layout_height="0dp">

                <TextView
                    android:id="@+id/movieTitle"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="8"
                    android:textSize="15dp"
                    android:textColor="#000"
                    android:textStyle="bold"
                    android:text="Long long long long long long long long  Title"/>

                <TextView
                    android:id="@+id/movieRating"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.7"
                    android:textSize="12dp"
                    android:textStyle="bold"
                    android:text="7.0" />

                <CheckBox
                    android:id="@+id/checkBox"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:scaleX="0.70"
                    android:scaleY="0.70" />

            </LinearLayout>

            <TextView
                android:id="@+id/movieDescription"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textSize="10dp"
                android:text="So many many many many many many many many many many many many description" />

        </LinearLayout>

    </LinearLayout>

</android.support.v7.widget.CardView>

感谢您的建议

【问题讨论】:

  • 只写XML标签是不够的。编辑您的帖子并包含属性。
  • 发布您的完整布局文件
  • 你在 relativeLayout 中使用 LinearLayout,并设置标题的权重.. 这就是它给出这个输出的原因
  • 我已经切换到LinearLayout,但问题依旧...
  • 我不知道为什么 height: wrap_content 不能按预期工作,我只是将其更改为匹配父项。总之,问题解决了。不想再花更多时间在这件奇怪的事情上。

标签: android xml android-linearlayout


【解决方案1】:

设置 RelativeLayout 属性以调整布局

Relative Layout Property are below
alignparentLeft,alignLeft
alignparentRight,alignRight
alignparentBottom,alignBottom
alignparentTop,alignTop

【讨论】:

  • 感谢 Divyesh 的帮助,看来这些属性不是关键。
【解决方案2】:

尝试使用像这样权重的线性布局

<?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">
    <ImageView
        android:layout_width="80dp"
        android:layout_height="100dp"
        android:src="@drawable/demo2"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:layout_height="0dp">
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:layout_weight="1"
                android:textColor="#000"
                android:textStyle="bold"
                android:text="Long Long Long Long Long Tittle"/>
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.25"
                android:text="7.0"/>
            <CheckBox
                android:layout_width="0dp"
                android:layout_weight=".25"
                android:layout_height="wrap_content" />
        </LinearLayout>
        <TextView
            android:layout_width="match_parent"
            android:text="so many many many many many many many many many many many many many  many many many description"
            android:layout_height="0dp"
            android:layout_weight="1"/>
    </LinearLayout>
</LinearLayout>

我得到了这个输出

【讨论】:

  • 感谢您的帮助,Jinesh。有趣的是你的代码运行完美,但是当我从你的代码中逐行复制并粘贴配置到我的代码时,问题仍然存在,我觉得自己被 Android Studio 愚弄了,
猜你喜欢
  • 1970-01-01
  • 2011-08-19
  • 1970-01-01
  • 1970-01-01
  • 2013-08-20
  • 1970-01-01
  • 2015-01-09
  • 1970-01-01
  • 2010-12-29
相关资源
最近更新 更多