View创建与xml之间的差异 - 使用代码查看创建

时间:2017-01-30 11:06:19

标签: java android android-layout

当我尝试以编程方式添加RelativeLayout以及直接使用xml视图时,会有所不同。

如果我在container_tabs中复制粘贴几个RelativeLayout,它就可以工作..

当我尝试给relativeLayout文件充气(与container_tabs完全相同)时,结果会有所不同,但不起作用。

我如何以编程方式填写视图

    @BindView(R.id.container_tabs)
    protected LinearLayout mLinearLayoutContainerTabs;

    private List<CustomBookingTab> mBookingTabsList = new ArrayList<>();

    private void initCustomButtonsTabs() {
       mBookingTabsList.add(new CustomBookingTab(this, EnumBookingTab.BOOKING_TAB_CALENDAR, R.drawable.ic_calendar_white));
       mBookingTabsList.add(new CustomBookingTab(this, EnumBookingTab.BOOKING_TAB_TIME, R.drawable.ic_clock));


       for (int i = 0; i < mBookingTabsList.size(); i++) {
            mLinearLayoutContainerTabs.addView(mBookingTabsList.get(i).getRelativeLayoutBookingTab(), i);
       }
    }

我如何从xml文件(CustomBookingTab构造函数)中扩展我的视图

mRelativeLayoutBookingTab = (RelativeLayout) inflater.inflate(R.layout.booking_tab, null);

主视图

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="-29dp"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="horizontal">

            <LinearLayout
                android:id="@+id/container_tabs"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:gravity="center"
                android:orientation="horizontal">

                <!-- Tab item -->
                <RelativeLayout
                    android:layout_width="0dp"
                    android:layout_height="70dp"
                    android:layout_gravity="center"
                    android:layout_weight="1">

                    <FrameLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <ImageView
                            android:layout_width="70dp"
                            android:layout_height="70dp"
                            android:layout_alignParentLeft="true"
                            android:background="@drawable/circle_blue_button_border_white"
                            android:padding="21dp"
                            android:src="@drawable/ic_calendar_white"
                            android:text="Button"
                            android:textColor="#FFFFFF"
                            android:textSize="30sp" />

                    </FrameLayout>

                </RelativeLayout>


            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

有没有好办法呢?有什么问题?

1 个答案:

答案 0 :(得分:0)

从XML添加视图和解剖膨胀视图有一些区别。

但是在准备XML时,如果是相对布局,则会使用如此多的相对属性将不同的视图一起设置为XML。

当我们使用相对布局动态添加视图时,我们应该像下面这样使用ADDRULE的选项:

 RelativeLayout relativeLayout  = new RelativeLayout(getActivity());
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        Button button1 = new Button(this);
        button1.setId(1);
        Button button2 = new Button(this);
        button2.setId(2);
        relativeLayout.addView(button1);
        relativeLayout.addView(button2);
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) button1.getLayoutParams();
        lp.addRule(RelativeLayout.RIGHT_OF, button2.getId());
        lp.addRule(RelativeLayout.LEFT_OF, button2.getId());
        lp.addRule(RelativeLayout.ALIGN_END, button2.getId());
        lp.addRule(RelativeLayout.ALIGN_LEFT, button2.getId());
        lp.addRule(RelativeLayout.ALIGN_BASELINE, button2.getId());
        lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, button2.getId());
        lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, button2.getId());
        lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, button2.getId());
        lp.addRule(RelativeLayout.ABOVE, button2.getId());
        lp.addRule(RelativeLayout.ALIGN_PARENT_TOP, button2.getId());
        lp.addRule(RelativeLayout.START_OF, button2.getId());
        lp.addRule(RelativeLayout.ALIGN_RIGHT, button2.getId());
        lp.addRule(RelativeLayout.ALIGN_PARENT_END, button2.getId());
        lp.addRule(RelativeLayout.END_OF, button2.getId());

几乎所有的相对布局属性都可以动态通过AddRull。