我想在一行中添加一些按钮,并在到达屏幕末尾时将其换行:
如果您了解CSS,我需要的是类似于display: inline-block
规则。
我只是在寻找XML解决方案,我想避免使用Java来测量屏幕和按钮,从而以编程方式将它们放置在下面。
这是我当前的代码,ConstraintLayout
中包含以下按钮:
<Button
android:id="@+id/boton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:text="Imagen" />
<Button
android:id="@+id/boton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="@id/boton1"
android:text="Play" />
<Button
android:id="@+id/boton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="@id/boton2"
android:text="Audio" />
<Button
android:id="@+id/boton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="@id/boton3"
android:text="Play"
android:onClick="playVideo" />
<Button
android:id="@+id/boton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="@id/boton4"
android:text="Youtube" />
<Button
android:id="@+id/boton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="@id/boton5"
android:text="Raw" />
答案 0 :(得分:1)
您是否尝试过使用gridLayout?
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="4">
<Button
android:id="@+id/boton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Imagen"
/>
<Button
android:id="@+id/boton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
/>
<Button
android:id="@+id/boton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Audio"
/>
<Button
android:id="@+id/boton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
android:onClick="playVideo"
/>
<Button
android:id="@+id/boton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Youtube"
/>
<Button
android:id="@+id/boton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Raw"
/>
</GridLayout>`
答案 1 :(得分:1)
自上次以来,我发现了您应该喜欢的这个图书馆! github.com/google/flexbox-layout
根据您的情况,您应该使用 flexWrap 属性:
此属性控制flex容器是单行还是多行,以及横轴的方向。可能的值为:
您将能够包装按钮。
安装
将以下依赖项添加到您的build.gradle文件中:
dependencies {
implementation 'com.google.android:flexbox:1.1.0'
}
用法
FlexboxLayout,用于扩展ViewGroup,如LinearLayout和RelativeLayout。您可以从布局XML中指定属性,例如:
<com.google.android.flexbox.FlexboxLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:flexWrap="wrap"
app:alignItems="stretch"
app:alignContent="stretch" >
<TextView
android:id="@+id/textview1"
android:layout_width="120dp"
android:layout_height="80dp"
app:layout_flexBasisPercent="50%"
/>
<TextView
android:id="@+id/textview2"
android:layout_width="80dp"
android:layout_height="80dp"
app:layout_alignSelf="center"
/>
<TextView
android:id="@+id/textview3"
android:layout_width="160dp"
android:layout_height="80dp"
app:layout_alignSelf="flex_end"
/>
</com.google.android.flexbox.FlexboxLayout>