Java 使用xml创建视图与使用代码创建视图之间的区别
当我试图以编程方式添加RelativeLayout时,与直接使用xml视图时,两者有所不同 如果我在容器中复制粘贴多个RelativeLayout,它会工作 当我尝试膨胀一个relativeLayout文件(与进入容器选项卡完全相同)时,结果是不同的,它不起作用 如何以编程方式填充视图Java 使用xml创建视图与使用代码创建视图之间的区别,java,android,android-layout,Java,Android,Android Layout,当我试图以编程方式添加RelativeLayout时,与直接使用xml视图时,两者有所不同 如果我在容器中复制粘贴多个RelativeLayout,它会工作 当我尝试膨胀一个relativeLayout文件(与进入容器选项卡完全相同)时,结果是不同的,它不起作用 如何以编程方式填充视图 @BindView(R.id.container_tabs) protected LinearLayout mLinearLayoutContainerTabs; private Lis
@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);
}
}
主视图
<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>
有什么好办法吗?有什么问题吗?从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动态添加。从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动态创建。正在创建的
mLinearLayoutContainerTabs
在哪里?你能提供更多的代码吗?我添加了mLinearLayoutContainerTabs
。这只是一个简单的活动,mLinearLayoutContainerTabs
在哪里创建?你能提供更多的代码吗?我添加了mLinearLayoutContainerTabs
。这只是一个简单的活动