【问题标题】:I can't add another row of buttons我无法添加另一行按钮
【发布时间】:2017-05-30 18:32:44
【问题描述】:

我希望我的片段看起来像它有 3 个按钮,可以在手机上垂直和水平地填充屏幕。我设法以某种方式做到了这一点,但我无法添加另一行按钮。这是我的布局...我做错了什么?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

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

            <Button
                android:id="@+id/button1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1" />

            <Button
                android:id="@+id/button2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1" />
            <Button
                android:id="@+id/button3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1" />


        </LinearLayout>

    </ScrollView>


</LinearLayout>

【问题讨论】:

    标签: java android xml layout


    【解决方案1】:

    滚动视图仅包含一个直接子级,因此您不能直接添加第二个子级,您必须执行以下操作

        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout 
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:fillViewport="true"
                android:layout_weight="1">
         <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    >
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    >
    
                    <Button
                        android:id="@+id/button1"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="1" />
    
                    <Button
                        android:id="@+id/button2"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="1" />
                    <Button
                        android:id="@+id/button3"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="1" />
    
    
                </LinearLayout>
            <!--add here second row and other continue same -->
    
        </LinearLayout>
            </ScrollView>
    
    </LinearLayout>
    

    注意:如果滚动视图只是您要使用的一个元素,那么您可以通过从根中删除当前的线性布局来使其成为根布局,并在相同情况下删除滚动视图的权重,最好的做法是最小化布局层次结构以提高性能

    【讨论】:

    • 谢谢它解决了我的问题,但我还有一件事。即使滚动视图有效,我也看不到最底部的一半按钮。它只是没有一路下降。
    • 在滚动视图标签中添加 android:fillViewport="true" 并检查
    【解决方案2】:

    第一个注意事项:滚动视图移除权重属性的影响 从布局中删除滚动视图元素

    第二点:每个按钮的制作

    <Button ...
     android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" />
    

    因为 0dp 让 Android 系统计算按钮的高度是一样的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-22
      • 1970-01-01
      • 2023-03-06
      • 2020-12-24
      • 1970-01-01
      • 2018-09-08
      • 2021-10-07
      • 1970-01-01
      相关资源
      最近更新 更多