Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

How to arrange items inside of LinearLayout to match to the parent width on android

I have some items inside of my LinearLayout and what I want to do is, arrange the items vertically in a way that it would spread evenly with default margin across the items.

This is what I have now :

enter image description here

So All the FAB's inside of the linear layout doesn't spread evenly, and even if I set a layout_marginStart it could cause some problems due to different resolutions across the android models.

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <FrameLayout
            android:layout_marginTop="25dp"
            android:layout_width="25dp"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|right">
            <com.google.android.material.floatingactionbutton.FloatingActionButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:elevation="0dp"
                app:backgroundTint="@color/gray"
                app:elevation="0dp"
                android:src="@android:color/transparent" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textSize="10sp"
                android:text="S"
                android:elevation="16dp"
                android:textColor="@android:color/white"/>
        </FrameLayout>
question from:https://stackoverflow.com/questions/65896559/how-to-arrange-items-inside-of-linearlayout-to-match-to-the-parent-width-on-andr

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this instead of FrameLayout:

<RelativeLayout
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:layout_marginTop="25dp">
    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:elevation="0dp"
        android:src="@android:color/transparent"
        app:backgroundTint="@color/gray"
        android:layout_centerInParent="true"
        app:elevation="0dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:elevation="16dp"
        android:text="S"
        android:textColor="@android:color/white"
        android:textSize="10sp" />
</RelativeLayout>

The children of a LinearLayout spread evenly when they have android:layout_width="0dp" and android:layout_weight="1"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...