简体   繁体   中英

ScrollView that shows only 5 buttons

I have a ScrollView with buttons inside. The buttons are part of a LinearLayout inside of my ScrollView.

I want my ScrollView to display only 5 buttons at a time. The height of each button should by 1/5 of the screen width.

Can I do that without creating a custom button and override OnMeasure method?

This is my layout:

  <?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"
    tools:context=".MainActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical"
       android:weightSum="5">

       <Button
           android:layout_width="match_parent"
           android:layout_height="0dp"
           android:layout_weight="1"
           android:background="#FF0000"/>
       <Button
           android:layout_width="match_parent"
           android:layout_height="0dp"
           android:layout_weight="1"
           android:background="#FFFF00"/>
       <Button
           android:layout_width="match_parent"
           android:layout_height="0dp"
           android:layout_weight="1"
           android:background="#FFFFFF"/>
       <Button
           android:layout_width="match_parent"
           android:layout_height="0dp"
           android:layout_weight="1"
           android:background="#AcCDFE"/>
       <Button
           android:layout_width="match_parent"
           android:layout_height="0dp"
           android:layout_weight="1"
           android:background="#000000"/>
       <Button
           android:layout_width="match_parent"
           android:layout_height="0dp"
           android:layout_weight="1"
           android:background="#00FF00"/>
       <Button
           android:layout_width="match_parent"
           android:layout_height="0dp"
           android:layout_weight="1"
           android:background="#0000FF"/>
       <Button
           android:layout_width="match_parent"
           android:layout_height="0dp"
           android:layout_weight="1"
           android:background="#FF00FF"/>
       <Button
           android:layout_width="match_parent"
           android:layout_height="0dp"
           android:layout_weight="1"
           android:background="#00FFFF"/>
       <Button
           android:layout_width="match_parent"
           android:layout_height="0dp"
           android:layout_weight="1"
           android:background="#ABCD94"/>
   </LinearLayout>

    </ScrollView>

</android.support.constraint.ConstraintLayout>

If you can not find the way for implement it using XML . Here is a solution using code

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {

        final ConstraintLayout rootLayout = findViewById(R.id.rootLayout);
        rootLayout.post(new Runnable() {
            @Override
            public void run() {
                // wait until we able to get rootLayout height then calculate and update button height
                int rootLayoutHeight = rootLayout.getHeight();

                LinearLayout linearLayout = findViewById(R.id.linearlayout);
                for (int i = 0; i < linearLayout.getChildCount(); i++) {
                    Button button = (Button) linearLayout.getChildAt(i);
                    LinearLayout.LayoutParams layoutParams =
                            (LinearLayout.LayoutParams) button.getLayoutParams();
                    layoutParams.height = rootLayoutHeight / 5;
                    button.setLayoutParams(layoutParams);
                }
            }
        });
    }
}

XML

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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <LinearLayout
            android:id="@+id/linearlayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#FF0000"
                />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#FFFF00"
                />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:background="#FFFFFF"
                />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:background="#AcCDFE"
                />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:background="#000000"
                />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:background="#00FF00"
                />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:background="#0000FF"
                />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:background="#FF00FF"
                />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:background="#00FFFF"
                />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:background="#ABCD94"
                />
        </LinearLayout>

    </ScrollView>

</android.support.constraint.ConstraintLayout>

It have 1 disadvantage is: When you use rootLayoutHeight/5 , it will return a float but you can not set float to button height (because layoutParams.height require int ) => you will lose some small value

删除android:weightSum="5"LinearLayoutandroid:layout_weight="1"Button在你的XML文件,并设置所有按钮的高度从Java文件=屏幕高度/ 5的运行时间。

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