简体   繁体   中英

How to make an Android horizontal LinearLayout with children that have a percentage width?

I've been searching this for a while and can't seem to quite grasp it. I know that this is probably done by using android:layout_weight , but I can't seem to figure it out.

What I want to accomplish is to have a horizontal LinearLayout that stretches the width of its parent's container and wraps the height of its child views. This will be used for a music player for my app. For the child views, I want the following...

Four ImageViews in a row, each set to 15% of the width (previous button, play/pause button, next button, and playlist button). Then after these, I will have some type of view to represent the progress of the song you are listening to which will fill the remaining width (obviously 40% of the width).

My icons will be square, and I need the progress bar to be the same height as the icons. Below is a screenshot from my mobile site, and you can see what I'm trying to recreate by looking at the footer of the site.

在此处输入图片说明

Do it like this... total weightSum of 100, and you can make the desired distribution

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="horizontal" 
   android:weightSum="100">

   BUTTON 1 layout_weight=15
   BUTTON 2 layout_weight=15
   BUTTON 3 layout_weight=15
   BUTTON 4 layout_weight=15

   ProgressBar layout_weight=40

</LinearLayout>

Also you can check this link about weight distribution http://developer.android.com/guide/topics/ui/layout/linear.html

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