【问题标题】:How can I create this layout that spans multiple rows using Android LinearLayout?如何使用 Android LinearLayout 创建跨越多行的布局?
【发布时间】:2018-09-24 22:33:57
【问题描述】:

我该怎么做才能使用 LinearLayout 在此布局中调出这些 ImageButton?我已经尝试过使用 TableLayout,但它对我不起作用,因为并非所有带有 ImageButtons 的行都显示为它的子项。

Layout

【问题讨论】:

    标签: android android-linearlayout imagebutton android-imagebutton


    【解决方案1】:

    这可以通过在布局中嵌套布局来实现。提示;

    <LinearLayout
     android: orientation = "vertical">
         <LinearLayout
          android: orientation = "horizontal">•
              <Button.../>
              <Button.../>
              <Button.../>
              <Button.../>
         </LinearLayout>
    
         <LinearLayout
          android: orientation = "horizontal">
              <Button.../>
              <Button.../>
              <Button.../>
              <Button.../>
         </LinearLayout>
         <RelativeLayout>
              <LinearLayout
               android: orientation = "horizontal"
               android: id = @+id/"abc">
                  <Button.../>
                  <Button.../>
                  <Button.../>
              </LinearLayout>
              <LinearLayout
               android: orientation = "horizontal">•
                  <Button.../>
                  <Button.../>
                  <Button.../>
              </LinearLayout>
                  <Button...
                   android: layout_toRightOf=" @id/abc"/>
         </RelativeLayout>
    
    
    </LinearLayout>
    

    您还可以在 ConstraintLayout 中嵌套布局。这一切都取决于你想要达到的结果的方式

    【讨论】:

    • 感谢您的快速回复!但是对于RelativeLayout 和2 个LinearLayouts 作为它的孩子它不会工作,因为你不能使用layout_weight 和2 个LinearLayouts。这意味着您不能告诉他们应该占用相同的空间。
    • 你是对的,但还有另一种方法可以做到这一点。最简单的就是将RelativeLayout改为LinearLayout,并分配布局权重,
    • 是的,这就是我在下面的回答中所做的。而且效果很好!非常感谢!
    【解决方案2】:

    经过一番折腾,我现在设法设计了上面的布局。当然,令人吃惊的是有很多嵌套的线性布局,但至少我做到了。 也许其他人有避免这些嵌套线性布局的想法,如果没有,那么我就这样吧,因为无论如何它都实现了它的目的。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">
    
            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1" />
    
            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1" />
    
            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1" />
    
            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1" />
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">
    
            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1" />
    
            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1" />
    
            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1" />
    
            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1" />
        </LinearLayout>
    
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:orientation="horizontal">
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="3"
                android:orientation="vertical">
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:orientation="horizontal">
    
                    <Button
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1" />
    
                    <Button
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1" />
    
                    <Button
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1" />
                </LinearLayout>
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:orientation="horizontal">
    
                    <Button
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1" />
    
                    <Button
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"/ >
    
                    <Button
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1" />
                </LinearLayout>
            </LinearLayout>
    
            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"/>
        </LinearLayout>
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-29
      • 2017-11-08
      • 2014-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多