简体   繁体   中英

Android: TextView and Button on Same Line in ExpandableListView

So I have an ExpandableListView and basically I have some text (person's name), and then I want a button on the same row (towards the right side if possible). Currently, this code in my XML file it has the text on one line, and then the button on the next line, I've played around with it but can't figure out how to get it both on one line.

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dip"
android:orientation="horizontal"
android:weightSum="1" >

<TextView
android:id="@+id/lblListItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />

<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_weight="0.8"
android:textSize="11dp"
android:text="Checkout"
android:id="@+id/checkout" />

</LinearLayout>

This will do the trick.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="55dip"
    android:orientation="horizontal" // Edited here
    android:weightSum="1" >  // here
 
<TextView
    android:id="@+id/lblListItem"
    android:layout_width="match_parent"  //and here
    android:layout_height="35dp"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:gravity="center_vertical"
    android:layout_weight="0.2" // here
    android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="match_parent" // and here
    android:layout_height="wrap_content"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:layout_weight="0.8" // and here
    android:textSize="12dp"
    android:text="Checkout"
    android:id="@+id/checkout" />

</LinearLayout>

Make sure of 2 things when using weightsum:

  1. if orientation is horizontal, width of all child views to be displayed in a row will need to have "match_parent" and some value for layout_weight.

  2. 0.2 will give 80% of horizontal width of parent and 0.8 will give 20%.

You have

android:orientation="vertical"

in your parent LinearLayout which stacks elements from top to bottom (naturally). Remove that line (since horizontal is the default orientation for LinearLayout ). Then you would need to use weight or margin to get it positioned where you want.

Another option would be to replace your LinearLayout with a RelativeLayout . This will allow you to use android:alignParentRight="true" on your Button to put it on the right side of the screen ( RelativeLayout puts Views at the top-left by default).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM