Problem with XML Linearlayout

Asked

Viewed 215 times

4

I’m having a little problem with my layout. I wanted to insert a button that would be at the bottom corner on the right side 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>

inserir a descrição da imagem aqui

  • 1

    You have to separate the layout of the items in the layout of Activity

  • It’s just like @ramaral said, you gotta have a layout elemento_lista.xml and another layout for Activity

2 answers

1

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

To resolve this issue, you must remove the Button and the LinearLayout of your xml (each item in the list) and move to your Activity or Fragment that contains your 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>
  • in my case this a little complicated, because the database automatically creates a list.

  • 1

    But you pass this xml file somewhere to create this list, right? And somewhere this ListView need to be declared. If you can put snippets of code, it would help!

  • My database is based on this: https://mega.co.nz/#! Lbuw3kaq! fqzesccUGZkweHGaMS45Xm2qPz_zYon8t4EdhoJ9Qjw

  • The problem is that in the given example, you are using a ListActivity. To use what you want, you must switch to a Activity standard and use a ListView within it.

1


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

The layout for each element of 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 characteristics of the button I added also android:layout_gravity="right". If you are building Listview with an Adapter you only need to refer to the layout of each item and the listview will appear as you want. In short, you need to have 2 . xml files instead of one.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.