Austin Meyers
Austin Meyers

Reputation: 153

LinearLayout Beneath LinearLayout full width?

I have an ImageView and a TextView that are the only items in my Android UI. Currently the TextView is in the same LinearLayout as the ImageView, so the text sometimes runs to 2 lines. I want to get the text on its own line, with the full width of the parent element so it will be the whole width of the screen. So far everything I've tried moves the ImageView outside the window or makes it very small.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:weightSum="4"
    android:gravity="center"
    tools:context=".MainActivity"
    android:baselineAligned="false">

    <LinearLayout
        android:layout_width="0dp"
        android:orientation="vertical"
        android:layout_weight="2"
        android:gravity="center_vertical"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/activity_main_imageview_gototags"
            android:src="@drawable/img_main_gototags"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop"
            android:onClick="imageViewGoToTagsOnClickOnClick"
            android:adjustViewBounds="true"/>

        <TextView
            android:id="@+id/activity_main_textview_tagline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/activity_main_textview_tagline"
            android:textColor="@color/dark_blue"
            android:gravity="center"
            android:textSize="30sp"
            android:layout_gravity="center_horizontal" />
    </LinearLayout>
</LinearLayout>

Upvotes: 1

Views: 777

Answers (2)

dora
dora

Reputation: 2077

May be this is what you want

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >


    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5" />

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


        <ImageView
            android:id="@+id/activity_main_imageview_gototags"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:onClick="imageViewGoToTagsOnClickOnClick"
            android:scaleType="centerCrop"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

    <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5" />
    </LinearLayout>

<TextView
    android:id="@+id/activity_main_textview_tagline"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:maxLines="1"
    android:singleLine="true"
    android:gravity="center"
    android:text="lfhjdhkjfhkjdhkjfhkdhkjfhdkshfkhskahdfkjdhkhkjfhdk"
    android:textColor="@android:color/holo_blue_bright"
    android:textSize="30sp" />

</LinearLayout>

I hope it helps.let me know if it did not.

Upvotes: 2

rahul.taicho
rahul.taicho

Reputation: 1379

Don't give the parent(LinearLayout) of your ImageView a width of 0dp make it match_parent instead and the same for your TextView also there's a property on text views android:maxLines that corresponds to how many lines at max can it go to, set it to 1 and if you need truncation you can also use android:ellipsize on the text view.

Upvotes: 0

Related Questions