最近我遇到了以编程方式包含布局的一个问题。
情境:
framelayout
。mainlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ly_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AppintroActivity">
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
sublayout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<RelativeLayout
android:id="@+id/ly_bottom_holder"
android:layout_width="match_parent"
android:gravity="bottom"
android:layout_height="@dimen/_50sdp"
android:layout_gravity="bottom">
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/dull_black" />
<TextView
android:id="@+id/txt_skip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/_10sdp"
android:padding="@dimen/_5sdp"
android:text="Skip"
android:textAllCaps="true"
android:textColor="@color/white" />
<ImageView
android:id="@+id/img_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:padding="@dimen/_5sdp"
android:src="@drawable/forward" />
<LinearLayout
android:id="@+id/dots"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="horizontal" />
</RelativeLayout>
</FrameLayout>
这就是我使用java
的方式LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
View bottomlayout = inflater.inflate(R.layout.bottom,(ViewGroup) findViewById(R.id.frame));//bottom is mysublayout.xml
ly_root.addView(bottomlayout);//ly_root is my mainlayout.xml's root element's id
所以请给我一个建议,我怎样才能通过使用java代码获得与使用xml完全相同的结果。
答案 0 :(得分:1)
我认为你必须使用&#34; LayoutParams &#34;添加视图到布局。像这样:
RelativeLayout.LayoutParams par=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
par.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
ly_root.addView(bottomlayout,par);
答案 1 :(得分:1)
RelativeLayout layout = findViewById(R.id.my_relative_layout);
View frame = getLayoutInflater().inflate(R.layout.sub_layout,null);
lp = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT); // You might want to tweak these to WRAP_CONTENT
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layout.addView(frame, lp);
答案 2 :(得分:0)
您必须在参数中应用RelativeLayout
规则并将其与参数一起添加,您可以执行以下操作:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
ly_root.addView(bottomlayout, params);
如果您在执行方面遇到问题,请告诉我。