如何使用 Android LinearLayout 创建跨多行的布局?
How can I create this layout that spans multiple rows using Android LinearLayout?
我该怎么做才能使用 LinearLayout 在此布局中调出这些 ImageButton?我已经用 TableLayout 试过了,但它对我不起作用,因为并不是所有以 ImageButtons 作为其子项的行都显示了。
Layout
这可以通过在布局中嵌套布局来实现。提示;
<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 中嵌套布局。这完全取决于您想要达到结果的方式
经过一番折腾,我现在已经设计出了上面的布局。当然,引人注目的是有很多嵌套的线性布局,但至少我做到了。
也许其他人有避免这些嵌套线性布局的想法,如果没有,那么我就这样吧,因为它无论如何都能达到目的。
<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>
我该怎么做才能使用 LinearLayout 在此布局中调出这些 ImageButton?我已经用 TableLayout 试过了,但它对我不起作用,因为并不是所有以 ImageButtons 作为其子项的行都显示了。
Layout
这可以通过在布局中嵌套布局来实现。提示;
<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 中嵌套布局。这完全取决于您想要达到结果的方式
经过一番折腾,我现在已经设计出了上面的布局。当然,引人注目的是有很多嵌套的线性布局,但至少我做到了。 也许其他人有避免这些嵌套线性布局的想法,如果没有,那么我就这样吧,因为它无论如何都能达到目的。
<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>