【问题标题】:How do I programmatically distribute child views evenly in a horizontal linear layout?如何以编程方式在水平线性布局中均匀分布子视图?
【发布时间】:2020-10-23 15:32:01
【问题描述】:

我有这样的看法:

<?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 添加到膨胀视图,但似乎仍然不起作用。我还面临栏和下一个图标之间的间距问题。但如果我删除边距,我无法将文本置于图标下方。

这就是我希望看到的视图

我如何做到这一点?

【问题讨论】:

    标签: android android-layout android-view


    【解决方案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>
    

    本例将创建 3 个占据整个屏幕宽度的按钮,每个按钮将占据屏幕宽度的 1/3。

    以编程方式执行此操作:

    LinearLayout layout = findViewById(R.id.yourLayoutID);
    Button b = new Button(); // Or, if you want something more complicated than a simple object, you can do View v = inflater.inflate(R.layout.your_layout, null);
    LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);
    p.weight = 1;
    b.setLayoutParams(p);
    layout.addView(b);
    

    这就是一般的想法

    【讨论】:

    • 感谢您的回答,但考虑到我提到的其他问题,我询问了以编程方式执行此操作。
    • 编辑了我的回复以包括如何以编程方式。让我知道是否有帮助
    • 抱歉回复晚了。我正在这样做(膨胀和添加)!但是我无法将栏和下面的文本处理成我想要的位置,就像我画的那样:(
    • 好吧,我认为最简单的方法是创建一个布局文件,根据需要放置文本和图片,我认为具有水平方向的 LinearLayout 或具有 layout_below 的 relativeLayout 以根据需要放置它们并且然后给它们充气
    猜你喜欢
    • 2014-12-17
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    • 2011-05-05
    相关资源
    最近更新 更多