Android:将从 XML 布局膨胀的 View 添加到尚未定义任何宽度的 View 组

标签 android xml layout layout-inflater viewgroup

我的自定义 View 组类存在布局问题。

我在构造函数中调用了 init()。在我的 init() 函数中,我从一个 xml 文件扩展 View 组布局,该文件包含一个 LinearLayout,我向其中添加了几个其他 View 。

我正在使用 spacer (View layout_width="0"and layout_weight="1") 来平均分配项目。

问题就在这里:当我添加 child 时,布局仍然没有定义任何宽度。所以垫片的大小都是 0,实际上不会平均放置“line_dot”项目。我怎样才能以某种方式更新它们的大小?

public class SliderSwitch extends FrameLayout {

...
    public SliderSwitch(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context)
    {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.slider_switch,this);


        sliderLayout = (LinearLayout) findViewById(R.id.sliderLayout);
        labelLayout = (LinearLayout) findViewById(R.id.labelLayout);


        // add one spacer
        View spacer = (View) inflate(context,R.layout.spacer, null);
        sliderLayout.addView(spacer);

        // setup the view depending on how many elements there are
        for (int i=1;i<numberOfElements-1;i++) {

            ImageView lineDot = (ImageView) inflater.inflate(R.layout.slider_switch_line_dot, null);
            sliderLayout.addView(lineDot);

            View spacer = (View) inflate(context,R.layout.spacer, null);
            sliderLayout.addView(spacer);
        }

    }
... 
}

这是垫片的 xml 文件:

<View
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_weight="1" />

这是我的 View 组布局

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/relLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:orientation="horizontal" >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/left_bg" />

            <LinearLayout
                android:id="@+id/sliderLayout"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/middle_bg"
                android:gravity="center"
                android:orientation="horizontal" >

               <!-- <include layout="@layout/spacer"/> -->

            </LinearLayout>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/right_bg" />
        </LinearLayout>

        <ImageButton
            android:id="@+id/sliderKnob"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="0dp"
            android:background="@null"
            android:src="@drawable/knob" />
    </RelativeLayout>

</LinearLayout>

最佳答案

我现在找到了解决方案。您必须再次在代码中设置 LayoutParams(即使它们已经在 xml 文件中定义),然后一切都整齐地间隔开,这是应该的。似乎是一个错误。但是这个解决方案有效:

    // add one spacer
    View spacer = (View) inflate(context,R.layout.spacer, null);
    spacer.setLayoutParams(new LinearLayout.LayoutParams(1,LayoutParams.WRAP_CONTENT,1.0F));
    sliderLayout.addView(spacer);

    // setup the view depending on how many elements there are

    for (int i=1;i<numberOfElements-1;i++) {

        ImageView lineDot = (ImageView) inflater.inflate(R.layout.slider_switch_line_dot, null);

        sliderLayout.addView(lineDot);

        spacer = (View) inflate(context,R.layout.spacer, null);
        spacer.setLayoutParams(new LinearLayout.LayoutParams(1,LayoutParams.WRAP_CONTENT,1.0F));
        sliderLayout.addView(spacer);
    }

关于Android:将从 XML 布局膨胀的 View 添加到尚未定义任何宽度的 View 组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13734824/

相关文章:

java - 如何在有效的 SOAP 请求 xml 模板中填充值

java - 如何通过简单访问节点来处理 Java 中的 XML?

css - 通过 CSS 进行流体布局 block 定位,无需任何高度知识即可响应式布局网站

android - 是否可以针对不同的产品口味使用不同的 build.gradle?

android - 返回随机数而不是 jsonobject 中 strings.xml 中的字符串值

android - TeamCity、Android、Gradle 脚本失败

android - 在 ANDROID 布局中动态添加 TextView

android - 如何在模拟器上显示 map

python - 如何使用 Selenium Python 从 XML URL 中按 tagName 提取元素

android - 如何摆脱 tabhost 中的水平滚动条?