Problem with XML LinearLayout

4

I have a small problem with my layout . I wanted to insert a button that would be in the lower right-hand corner of the layout , but when I do this the button is added to each item in the list.

Note: This list is generated by a database

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".BancoDados.Principal">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingTop="20dp"
        android:paddingBottom="20dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:id="@+id/linearLayout">

        <ImageView
            android:id="@+id/img_rest"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:padding="5dp"
            android:background="@drawable/l" />

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

            <TextView
                android:id="@+id/tv_nome"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="10dp"
                android:paddingTop="2dp"
                android:textSize="16sp"
                android:textStyle="bold"
                android:singleLine="true"
                android:text="@string/nome" />

            <TextView
                android:id="@+id/tv_professor"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="10dp"
                android:paddingTop="2dp"
                android:singleLine="true"
                android:text="@string/professor" />

            <TextView
                android:id="@+id/tv_periodo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="10dp"
                android:paddingTop="2dp"
                android:textSize="12sp"
                android:singleLine="true"
                android:text="@string/periodo" />

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button" />
</LinearLayout>

</RelativeLayout>

    
asked by anonymous 03.03.2015 / 15:35

2 answers

1

I've already had this problem ... to get the look you want, you should create two layout files, one for the elements of your list, and another for the activity.

The layout for each element on your list would be something like:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingTop="20dp"
        android:paddingBottom="20dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:id="@+id/linearLayout">

        <ImageView
            android:id="@+id/img_rest"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:padding="5dp"
            android:background="@drawable/l" />

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

            <TextView
                android:id="@+id/tv_nome"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="10dp"
                android:paddingTop="2dp"
                android:textSize="16sp"
                android:textStyle="bold"
                android:singleLine="true"
                android:text="@string/nome" />

            <TextView
                android:id="@+id/tv_professor"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="10dp"
                android:paddingTop="2dp"
                android:singleLine="true"
                android:text="@string/professor" />

            <TextView
                android:id="@+id/tv_periodo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="10dp"
                android:paddingTop="2dp"
                android:textSize="12sp"
                android:singleLine="true"
                android:text="@string/periodo" />

        </LinearLayout>
 </LinearLayout>

The layout of your activity / fragment would be:

    

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:layout_gravity="right"
    android:id="@+id/button" />

In the button features I also added android: layout_gravity="right" . If you are building the ListView with an Adapter you will only have after referring to the layout of each item and the listview will appear as you want. In short, you must have 2 .xml files instead of one.

    
04.03.2015 / 00:27
1

The problem is that you are putting Button inside the item in your list and when you inflate it, you will have a button for each item.

To solve this problem, you must remove Button and LinearLayout of your xml (each item from the list) and move to its Activity or Fragment that contains its ListView :

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

    <ListView
        android:id="@+id/placeholder_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <Button
        android:id="@+id/my_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"/>  

</RelativeLayout>
    
03.03.2015 / 15:59