Android 如何以编程方式在水平线性布局中均匀分布子视图?
我有这样的看法:Android 如何以编程方式在水平线性布局中均匀分布子视图?,android,android-layout,android-view,Android,Android Layout,Android View,我有这样的看法: <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://s
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/sample_icon_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:src="@drawable/active_state"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/sample_item_bar_iv"
android:layout_width="128dp"
android:layout_height="1dp"
android:background="@color/brown_grey"
app:layout_constraintBottom_toBottomOf="@id/sample_icon_iv"
app:layout_constraintStart_toEndOf="@id/sample_icon_iv"
app:layout_constraintTop_toTopOf="@id/sample_icon_iv"
/>
<TextView
android:id="@+id/leave_planner_item_date_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="26 March"
app:layout_constraintEnd_toStartOf="@+id/sample_item_bar_iv"
app:layout_constraintStart_toStartOf="@+id/sample_icon_iv"
app:layout_constraintTop_toBottomOf="@+id/sample_icon_iv"
tools:text="26 March" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
我需要将这个视图放大x倍,并以水平滚动的方式显示均匀分布的视图。在添加到父视图之前,我尝试将权重1添加到膨胀视图中,但似乎仍然不起作用。我还面临着工具栏和下一个图标之间的间距问题。但是如果我删除了边距,我就无法将图标下的文本居中
这就是我希望看到的观点
如何实现这一点?将视图包装为线性布局。确定要有多少个孩子,然后将其设置为:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="3"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
这就是总体思路将您的视图用线性布局进行包装。确定要有多少个孩子,然后将其设置为:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="3"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
这是总体思路谢谢你的回答,但考虑到我提到的其他问题,我询问了如何以编程方式进行此操作。编辑了我的回答,以包括如何以编程方式进行此操作。请让我知道是否有助于教授回复晚了。我正是这样做的(充气和添加)!但我无法将下面的条形图和文本精确地放置在我想要的位置,如我所画的图片所示:(好的,最简单的方法,在我看来是创建一个布局文件,根据你的需要放置文本和图片,我认为水平方向的线性布局或与下面的布局相对应的布局,根据你的需要放置它们,然后膨胀它们谢谢你的回答,但考虑到另一个问题,我要求以编程方式进行此操作我提到了。编辑了我的回复,以包括如何以编程方式进行。让我知道它是否有助于回答迟到。我正在这样做(膨胀和添加)!但我无法将下面的栏和文本精确地处理到我想要的位置,如我绘制的图片所示:(在我看来,最简单的方法是创建一个布局文件,根据您的需要放置文本和图片,我认为使用水平方向的线性布局或与下面的布局相对应的布局,以根据您的需要放置它们,然后将它们膨胀。)