TextView disconnect in LinearLayout

I am trying to add two different text boxes with different heights, for example:

<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>

      

And the result will be the following:

enter image description here

(Ignore the shadow above. I cropped the image and the action bar shadow)

The height of a linear layout is determined by smaller text, not large. What for? And how can I fix it? thanks in advance

+3


source to share


5 answers


Make the text height wrap_content

it will solve the problem



android:id="@+id/newsfeed_ad_title"
    android:layout_width="0px"
    android:layout_height="wrap_content"

      

+4


source


try it,

<?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>

      



If that doesn't solve your problem, open the dimen.xml file .

Hope this is helpful ... thanks

+1


source


Do you want the left text image (with id newsfeed_ad_title) not to be cut off correctly? Change android: layout_height to "wrap_content"

0


source


Try to install match_parent

newsfeed_ad_info_button

TextView:

<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" />

      

Note: Also use dp instead of px: http://developer.android.com/guide/practices/screens_support.html

0


source


The problem is that the first one TextView

(with an id newsfeed_ad_title

) has a height match_parent

. This means it will first LinearLayout

calculate its preferred height and then TextView

take that exact height.
Providing wrap_content

for the first TextView

will solve the problem because this way it will LinearLayout

first ask both children to calculate their desired height and then set it accordingly.

<TextView
android:id="@+id/newsfeed_ad_title"
android:layout_width="0px"
android:layout_height="wrap_content"
... //the rest is unmodified

      

0


source







All Articles