TextView在LinearLayout中被裁剪了。

3

我正在尝试添加两个不同高度的文本视图,如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/single_margin"
android:layout_marginLeft="@dimen/double_margin"
android:layout_marginRight="@dimen/double_margin"
android:layout_marginTop="@dimen/single_margin"
android:baselineAligned="false"
android:gravity="center"
android:orientation="horizontal">


<TextView
    android:id="@+id/newsfeed_ad_title"
    android:layout_width="0px"
    android:layout_height="match_parent"
    android:layout_marginRight="28dp"
    android:layout_weight="3"
    android:fontFamily="sans-serif-light"
    android:gravity="center_vertical"
    android:singleLine="false"
    android:text="This is example text view that will mess up the height!"
    android:textColor="@color/dark_blue"
    android:textSize="@dimen/ad_title_text" />


<TextView
    android:id="@+id/newsfeed_ad_info_button"
    android:layout_width="0px"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight="2"
    android:background="@drawable/selector_rounded_box_light_blue"
    android:fontFamily="sans-serif-light"
    android:gravity="center"
    android:paddingBottom="@dimen/single_margin"
    android:paddingLeft="@dimen/double_margin"
    android:paddingRight="@dimen/double_margin"
    android:paddingTop="@dimen/single_margin"
    android:text="Learn More"
    android:textColor="@color/dark_blue"
    android:textSize="@dimen/body_text" /> </LinearLayout>

结果如下:

在此输入图片描述

(不用在意上面的阴影。我裁剪了图片,阴影是来自操作栏)

线性布局的高度由较小的文本视图决定而不是较大的文本视图。为什么?如何解决?先谢谢了。


尝试在左侧的TextView上添加“android:layout_gravity =“fill””属性 - calvinfly
1
请问您能提供您的dimen.xml文件吗? - sahu
你的两个TextView的dimen值不同。 - Shoeb Siddique
1
将你的LinearLayout设置为match_parent。 - frost
5个回答

6
将TextView的高度设置为“wrap_content”将解决问题。
android:id="@+id/newsfeed_ad_title"
    android:layout_width="0px"
    android:layout_height="wrap_content"

我简直不敢相信我错过了那个。这就是我连续编码几个小时的结果。 - Sree

1

你想让左边的文本视图(ID为newsfeed_ad_title)不被裁剪吗?将android:layout_height更改为“wrap_content”


1

try this,

<?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:layout_marginBottom="@dimen/single_margin"
android:layout_marginLeft="@dimen/double_margin"
android:layout_marginRight="@dimen/double_margin"
android:layout_marginTop="@dimen/single_margin"
android:baselineAligned="false"
android:gravity="center"
android:orientation="horizontal" >

<TextView
    android:id="@+id/newsfeed_ad_title"
    android:layout_width="0px"
    android:layout_height="match_parent"
    android:layout_marginRight="28dp"
    android:layout_weight="3"
    android:fontFamily="sans-serif-light"
    android:gravity="center_vertical"
    android:singleLine="false"
    android:text="This is example text view that will mess up the height!"
    android:textColor="@color/dark_blue"
    android:textSize="@dimen/ad_title_text" />

<TextView
    android:id="@+id/newsfeed_ad_info_button"
    android:layout_width="0px"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:layout_weight="2"
    android:background="@drawable/selector_rounded_box_light_blue"
    android:fontFamily="sans-serif-light"
    android:gravity="center"
    android:paddingBottom="@dimen/single_margin"
    android:paddingLeft="@dimen/double_margin"
    android:paddingRight="@dimen/double_margin"
    android:paddingTop="@dimen/single_margin"
    android:text="Learn More"
    android:textColor="@color/dark_blue"
    android:textSize="@dimen/body_text" />

</LinearLayout>

如果这不能解决你的问题,那么请提供你的dimen.xml文件。
希望这对你有帮助...谢谢。

0
尝试将 newsfeed_ad_info_button TextView 设置为 match_parent
<TextView
    android:id="@+id/newsfeed_ad_info_button"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:layout_weight="2"
    android:background="@drawable/selector_rounded_box_light_blue"
    android:fontFamily="sans-serif-light"
    android:gravity="center"
    android:paddingBottom="@dimen/single_margin"
    android:paddingLeft="@dimen/double_margin"
    android:paddingRight="@dimen/double_margin"
    android:paddingTop="@dimen/single_margin"
    android:text="Learn More"
    android:textColor="@color/dark_blue"
    android:textSize="@dimen/body_text" />

注意:在编程中使用 dp 而不是 px: http://developer.android.com/guide/practices/screens_support.html


0
问题在于第一个 TextView(ID 为 newsfeed_ad_title 的那个)的高度为 match_parent。这意味着首先 LinearLayout 将计算其首选高度,然后 TextView 将占用完全相同的高度。
将第一个 TextView 设置为 wrap_content 将解决此问题,因为这样 LinearLayout 将首先要求两个子元素计算其所需高度,然后再相应地设置自己的高度。
<TextView
android:id="@+id/newsfeed_ad_title"
android:layout_width="0px"
android:layout_height="wrap_content"
... //the rest is unmodified

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接