Android layout width 50%

I am using relative layout to create Android UI in XML. The layout is centered around ImageView and TextView. I need these two elements to lie above and below each other. I want to make it so that these 2 elements take up 50% of the entire width. I tried LinearLayout with android:layout_weight=".5"

, but I couldn't center it both horizontally and vertically. Here is the relative layout I have, I want it to look like this, but with the image taking up 50% of the width, with 25% white space on either side. I also need to do this in XML. Any guidance is appreciated.

<RelativeLayout 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"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/activity_main_imageview_logo"
        android:src="@drawable/img_main_logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:scaleType="centerCrop"
        android:paddingBottom="12dp"
        android:onClick="imageViewGTTOnClick"
        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="28sp"
        android:layout_below="@+id/activity_main_imageview_logo"
        android:layout_centerHorizontal="true" />

 </RelativeLayout>

      

+3


source to share


1 answer


This should do the trick:



<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_logo"
            android:src="@drawable/img_main_logo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop"
            android:paddingBottom="12dp"
            android:onClick="imageViewGTTOnClick"
            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="28sp"/>
    </LinearLayout>

</LinearLayout>

      

+4


source







All Articles