android - 在 View 组中添加 View 时忽略 layout_margin

标签 android android-layout

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

首先,我固定了项目的宽度、高度和边距。

<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>

下面是在线性布局中添加 View 的代码

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);

使用上面的代码,我可以使项目适合屏幕,但是边距被忽略了。

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

最佳答案

关于android - 在 View 组中添加 View 时忽略 layout_margin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20519066/

相关文章:

java - 如何从 okhttp 的缓存中删除 url?

android - 改造和Moshi : parsing variable name objects

android - Firebase 中 IN 关键字的替代是什么?

android - 根据 ListView 的行高动态拉伸(stretch)图像

java - 如何根据屏幕尺寸调整按钮大小

android - 为什么在主要 Activity 关闭时远程服务会被破坏?

linux - Linux C 应用程序中的原始套接字与 Libpcap

android - Fragment中onCreateView和onViewCreated的区别

Android背景图片拉伸(stretch)

android - GridLayout 膨胀异常