简体   繁体   中英

Android Horizontal Scrollview with horizontal linear layout. How can fill the whole width?

im' doing some exercice to learn android progamming.

I want that every button in this horizontal layout is full widht.

So user can scroll it.

Here is my xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="140dp">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="Button1" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0"
                android:text="Button2" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="Button3" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="Button4" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="Button5" />

        </LinearLayout>
    </HorizontalScrollView>

</android.support.constraint.ConstraintLayout>

在此处输入图片说明

So button 1, and every other button have to scroll line in a gallery

You are using HorizontalScrollView so match_parent for width won't work.

You have to set the width of button programmatically to the width of screen width.

Try this

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
 <android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="140dp">
        <LinearLayout
            android:id="@+id/ll"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">
            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Button1" />
            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Button2" />
            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Button3" />
            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Button4" />
            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Button5" />
        </LinearLayout>
    </HorizontalScrollView>
</android.support.constraint.ConstraintLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
private LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    linearLayout=findViewById(R.id.ll);
    int width=getScreenWidth(Main2Activity.this);

    int childCount=linearLayout.getChildCount();
    for (int i=0;i<childCount;i++){
        Button button= (Button) linearLayout.getChildAt(i);
        button.setWidth(width);
    }
}
public static int getScreenWidth(Context context) {
    WindowManager windowManager = (WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics dm = new DisplayMetrics();
    windowManager.getDefaultDisplay().getMetrics(dm);
    return dm.widthPixels;
} }

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