简体   繁体   English

如何以编程方式(动态)将LinearLayout放在RelativeLayout内部?

[英]How to programmatically (dynamically) center a LinearLayout inside a RelativeLayout?

I want to dynamically center a LinearLayout view inside its RelativeLayout parent and so I want to use android.R.attr.layout_centerInParent. 我想将LinearLayout视图动态居中于其RelativeLayout父对象中,因此我想使用android.R.attr.layout_centerInParent。 How do I apply it? 我该如何申请? I tried the following but the child view does not appear at all. 我尝试了以下操作,但根本没有出现子视图。

child view 儿童观

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView android:id="@+id/text1"
        android:textSize="16sp"
        android:textStyle="bold"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView android:id="@+id/text2"
        android:textSize="16sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

parent view 家长观点

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parent_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center">

    <FrameLayout
        android:id="@+id/my_preview"
        android:layout_width="match_parent"
        android:layout_height="407dp" >
    </FrameLayout> 

    <include
        android:id="@+id/my_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="408dp"
        layout="@layout/my_bar" />

</RelativeLayout>

in my activity 在我的活动中

            LayoutInflater inflater = (LayoutInflater) this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            RelativeLayout rootView = (RelativeLayout) inflater.inflate(id, null);
            LinearLayout textBox = (LinearLayout) inflater.inflate(R.layout.child_view, null);
            TextView line2 = (TextView) textBox.findViewById(R.id.text2);
            line2.setText("hahahaha");
            TextView line1 = (TextView) textBox.findViewById(R.id.text1);
            line1.setText("rrrrrrrrrr");
            RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
            rlp.addRule(android.R.attr.layout_centerInParent);                  
            textBox.setLayoutParams(rlp);
            rootView.addView(textBox);
            setContentView(rootView);

I changed the child view to a RelativeLayout but still cannot see the child view. 我将子视图更改为RelativeLayout,但仍然看不到子视图。

For some reason, android.R.attr.layout_centerInParent fails. 由于某些原因,android.R.attr.layout_centerInParent失败。 I used 我用了

rlp.addRule(RelativeLayout.CENTER_IN_PARENT);   

and it worked! 而且有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM