简体   繁体   中英

Android buttons are cut off (LinearLayout horizontal)

In my app I am calling API to get list of people with some informations (address, phone numbers etc.). Under every phone number, I am creating programatically 3 buttons (add to contacts, edit and call). Problem is, that last button is cut off (small screen). I am using Linear Layout horizontal.

Is there any way to control size of screen and if needed, put last button to second line? When I rotate screen to landscape, I have enough space, so buttons should stay in one line.

Now, I am using horizontalScrollView with visible scrollbar. It's working, but I am not very satisfied with it.

Thanks for help.

I'm not really sure if you can do that with LinearLayout. But you could do that using FlowLayout. Check this link: https://github.com/ultimate-deej/FlowLayout-for-Android .

This layout moves the buttons to the next line if there is no space for them on the screen.

<LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal">
 <Button
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:weight="1" />
<Button
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:weight="1" />
</LinearLayout>

the weight attribute will automatically adjust your button size
make sure you set width to match parent for all buttons.

Best to create a new variant of the listitem layout for smaller screenshots:

  • layout-w200dp/listitem.xml : layout with 3 buttons on one line
  • layout/listitem.xml : layout with buttons on separate lines

Android will then choose the multiline layout when the current available width is smaller than 200dp. (Note that you can still tweak the 200 to a different value)

Alternatively you can also use an alternatieve linearlayout which does the wrapping for you: Flowlayout

This is probably more complicated to achieve that you wan't it to be but the best shot is to use some adapter based solution:

  1. GridView - this is the old solution, better go for the 2nd
  2. RecyclerView with StaggeredLayoutManager setup to your needs

Simple solution is using android:layout_weight="1" and android:layout_width="0dp " as params for each button in your LinearLayout but then they will fit the whole screen and take the same percent of the width, and if the screen is too small buttons might get cut off.

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