android - 只显示 5 个按钮的 ScrollView

标签 android android-layout

我有一个带有按钮的 ScrollView。这些按钮是我的 ScrollView 中 LinearLayout 的一部分。

我希望我的 ScrollView 一次只显示 5 个按钮。每个按钮的高度应为屏幕宽度的 1/5。

我可以在不创建自定义按钮和覆盖 OnMeasure 方法的情况下这样做吗?

这是我的布局:

  <?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>

最佳答案

如果您找不到使用XML 实现它的方法。这是使用代码的解决方案

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>

它有 1 个缺点是:当您使用 rootLayoutHeight/5 时,它会返回一个 float 但您不能将 float 设置为按钮高度(因为 layoutParams.height 需要 int)=> 你会失去一些小值

关于android - 只显示 5 个按钮的 ScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52626322/

相关文章:

android - 如何均匀对齐两个微调器?(%50-%50)

android - ContactsContract.Contacts 查询特定账户

java - Android自定义双击位置

android - webrtc android 应用程序中需要关键帧的第一个错误。如何降低传入视频的帧率?

android - 微调器下拉外边框

Android - 实现背景效果的调光

java - AutoCompleteTextView Item 选择触发事件

java - 在 Android "java"应用程序中执行 javascript 代码

android - 为什么android无法解析符号但它仍然有效?

Android 印地文字符软键盘?