Pergunta

Am trying to achieve the below layout on Android (Target API 18, min API 8) enter image description here

Am relatively new to Android & came up with below layout file (activity_main.xml). I know a bit about Linear versus Relative layout and would highly appreciate if anyone can provide some input in this direction.

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

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/task_name"
    android:textSize="14sp" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/textView1"
    android:ems="10"
    android:hint="@string/task_name_hint"
    android:inputType="text" />

<DatePicker
    android:id="@+id/dpResult"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/textView1"
    android:layout_marginTop="8dp" />

<RadioGroup
    android:id="@+id/priority_radios"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/dpResult"
    android:inputType="text"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/first"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/first_radio" />

    <RadioButton
        android:id="@+id/second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/second_radio"/>


    <RadioButton
        android:id="@+id/third"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/third_radio" />

<RadioButton
    android:id="@+id/none"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/none_radio" 
    android:checked="true" />

</RadioGroup>


<RadioGroup
    android:id="@+id/category_radios"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/priority_radios"
    android:inputType="text"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/long_category"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/first_radio" />

    <RadioButton
        android:id="@+id/short_category"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/second_radio"/>


    <RadioButton
        android:id="@+id/new_category"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/third_radio" />


</RadioGroup>

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/tag_name"
    android:textSize="14sp" />

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:entries="@array/tag_arrays"
    android:prompt="@string/tag_prompt" 
    android:layout_below="@id/category_radios" 
    android:layout_toRightOf="@id/textView2" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/spinner1"
    android:text="@string/create_task" />

</RelativeLayout>
Foi útil?

Solução

It seems a fairly easily layout. You can use a vertical parent linearlayout, and then several horizontal linearlayout for each row.

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

<LinearLayout
    android:id="@+id/line1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Task name"/>

    <EditText android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter task name"/>
</LinearLayout>

<LinearLayout android:id="@+id/line2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <!-- SECOND LINE -->

</LinearLayout>

<LinearLayout android:id="@+id/line3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <!-- THIRD LINE -->
</LinearLayout>

Outras dicas

I'd use a RelativeLayout as you do, for the container, then some LinearLayouts and RadioGroups (which inherit from LinearLayout)

Now this is the result I got (note that I left the central part empty) - no matter if I used a picture that looks like a combo for the textview (it's just a 9 patch I had handy) and the images don't match with yours...

enter image description here

By using this layout

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ccc"
    android:orientation="vertical"
    >
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1.5"
        android:layout_margin="4dp"
        >
        <ImageButton
            android:id="@+id/imgBack"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:src="@drawable/ic_launcher"
            android:layout_margin="4dp"
        />
        <ImageButton
            android:id="@+id/imgFore"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_toRightOf="@id/imgBack"
            android:layout_centerVertical="true"
            android:src="@drawable/ic_launcher"
            android:layout_margin="4dp"
        />
        <ImageButton
            android:id="@+id/imgExit"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_toRightOf="@id/imgFore"
            android:layout_centerVertical="true"
            android:src="@drawable/ic_launcher"
            android:layout_margin="4dp"
        />
        <ImageButton
            android:id="@+id/imgHome"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_toRightOf="@id/imgExit"
            android:layout_centerVertical="true"
            android:src="@drawable/ic_launcher"
            android:layout_margin="4dp"
        />
        <ImageButton
            android:id="@+id/imgFind"
            android:layout_width="32dp"
            android:layout_height="24dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:src="@drawable/ic_launcher"
            android:layout_margin="4dp"
        />
        <TextView
            android:id="@+id/txtAddress"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/imgHome"
            android:layout_toLeftOf="@id/imgFind"
            android:layout_centerVertical="true"
            android:background="@drawable/combo_opt_m"
            android:layout_margin="4dp"
        />
    </RelativeLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="7.5"
        android:background="#fff"
        >

    </LinearLayout>
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        >
        <ImageButton
            android:id="@+id/imgFoot"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:src="@drawable/ic_launcher"
            android:layout_margin="4dp"
        />
    </RelativeLayout>
</LinearLayout>

please note that, in my onCreate method, I used:

@Override
protected void onCreate(
    final Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    // Make this activity, full screen
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

    // Hide the Title bar of this activity screen
    getWindow().requestFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.form);
}

In order to make the app fullscreen

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top