在视图组中添加视图时,将忽略layout_margin

时间:2013-12-11 12:23:55

标签: android android-layout

我正在尝试将Horizo​​ntalScrollView与子LinearLayout视图一起使用。 我想要一个项目用边距填充屏幕,滚动后可以看到下一个项目。为了让你明白,我会用代码告诉你我尝试的一切。

首先,我修正了项目的宽度,高度和边距。

<com.example.kanbanboard.TaskListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="360dp"
    android:layout_height="360dp"
    android:layout_margin="10dp"
    android:background="@drawable/container">

    <TextView
        android:id="@+id/category"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textSize="20sp"
        android:layout_margin="10dp"/>
    <ListView
        android:id="@+id/list"
        android:layout_margin="10dp"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent" />
</com.example.kanbanboard.TaskListView>

此父级的布局为

<com.example.kanbanboard.MyHorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="1dp" // It seems that whether 1dp or fill_parent does not matter
    android:layout_height="1dp"
    tools:context=".MainActivity"
    tools:ignore="MergeRootFrame">

    <LinearLayout
        android:id="@+id/list_container"
        android:orientation="horizontal"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:background="#000000">
    </LinearLayout>

</com.example.kanbanboard.MyHorizontalScrollView>

以下是在线性布局中添加视图的代码

public void init(){
    LayoutInflater inflater = LayoutInflater.from(getContext());
    ViewGroup parent = (ViewGroup) getChildAt(0);
    //CategoryView is almost same with TaskListView
    CategoryView categoryList = (CategoryView) inflater.inflate(R.layout.category_layout, parent, false);
    TaskListView taskList = (TaskListView) inflater.inflate(R.layout.task_list_layout,
                                                            parent, false);
    categoryList.init(taskList);
    taskList.init();

    View[] children = new View[]{categoryList, taskList};
    ViewTreeObserver.OnGlobalLayoutListener listener = new MainLayoutListener(parent, children);
    getViewTreeObserver().addOnGlobalLayoutListener(listener);
}

class MainLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener{

    ViewGroup parent;
    View[] children;

    public MainLayoutListener(ViewGroup parent, View[] children){
        this.parent = parent;
        this.children = children;
    }

    @Override
    public void onGlobalLayout(){
        ScrollView me = ScrollView.this;
        me.getViewTreeObserver().removeGlobalOnLayoutListener(this);

        parent.addView(children[0]);
        parent.addView(children[1]);
    }
}

结果:http://i.imgur.com/zIe1gTN.png?1

边距未被忽略,但该项目不适合屏幕。 所以我尝试在layout_width和layout_height中使用fill_parent,但结果是

http://imgur.com/y2yNWmz&zIe1gTN#0

我还尝试使用LayoutParams设置宽度,高度和边距。

        ScrollView me = ScrollView.this;
        me.getViewTreeObserver().removeGlobalOnLayoutListener(this);

        int w = me.getMeasuredWidth() - 50 // for margin;
        int h = me.getMeasuredHeight() - 50;

        ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(w, h);
        params.setMargins(10, 10, 10, 10);

        parent.addView(children[0], params);
        parent.addView(children[1], params);

使用上面的代码,我可以将项目放到屏幕上,但忽略了边距。

我希望你理解我的问题。我该如何解决这种情况?

1 个答案:

答案 0 :(得分:-1)