johnyX
johnyX

Reputation: 101

XML layout 2 views in centre, one at top

I created linearlayout with two buttons in center (buttons 1 and 2), but I can't manage it to create the view 3 on top, which I need to make it fill whole width and adjust it's height by it (keep proportions). How do I create third view?

Image:

enter image description here

Code:

<?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:background="@drawable/bg"
              android:layout_gravity="center_horizontal"
              android:orientation="horizontal">

    <LinearLayout
        android:orientation="vertical"
        android:layout_gravity="center_vertical"
        android:layout_width="fill_parent" android:layout_height="wrap_content" >

        <ImageButton 
            android:background="@drawable/btn_one" 
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1.0" />

        <ImageButton 
            android:background="@drawable/btn_second" 
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1.0" /> 
    </LinearLayout>

Upvotes: 1

Views: 38

Answers (2)

Kishan patel
Kishan patel

Reputation: 214

try this code set height and width what size of image button you need and simple look what you need.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

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

    <ImageButton
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="0.2"
    android:orientation="vertical"
    android:gravity="center">


    <ImageButton
        android:layout_width="200dp"
        android:layout_height="50dp"/>

    <ImageButton
        android:layout_width="200dp"
        android:layout_height="50dp" />

</LinearLayout>

Upvotes: 2

Ali Elgazar
Ali Elgazar

Reputation: 777

I would recommend you use relative layouts, they are the closest thing in android to an "absolute layout" replace the button tags with Image button tags

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true">

</RelativeLayout>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button2"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="215dp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button3"
    android:layout_below="@+id/button2"
    android:layout_alignStart="@+id/button2" />


 </RelativeLayout>

Upvotes: 3

Related Questions