ViewStub没有显示?

时间:2015-01-13 14:55:06

标签: android android-layout layout relativelayout viewstub

我有一个包含一些视图和一个按钮的Relativelayout rl_root,当按下按钮时,调用了一个名为showConfigStubView的方法,它将使布局rl_root消失,并且viewStub应替换此viewGroup rl_root

在run ime中,当调用方法showConfigStubView时,viewGroup rl_root消失,viewStub不会显示。

showConfigStubView中调用View.gone以使rl_root消失,然后以编程方式Viewstub创建新的viewStub = new ViewStub(getActivity());,然后使用其参数创建一个realtiveLayout然后将ViewStub添加到新的RelativeLayout,然后使用各自的布局对viewStub进行充气。

请看下面的代码,方法showConfigStubView解释了我如何调用ViewStub

主要布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical|center_horizontal">

<RadioGroup 
    android:id="@+id/rg_mqtt_config_choice"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:checkedButton="@+id/rb_used_configs">
    <RadioButton 
        android:id="@+id/rb_used_configs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/rb_used_configs"/>
    <RadioButton
        android:id="@+id/rb_new_configs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/rb_new_configs" />
</RadioGroup>

<Button 
    android:id="@+id/btn_connect"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/rg_mqtt_config_choice"
    android:text="@string/btn_connect"/>

ViewStub

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<TextView 
    android:id="@+id/tvSubView_ip"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:paddingTop="10dp"
    android:text="@string/tvSubView_ip"/>
<EditText 
    android:id="@+id/etSubView_ip"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toEndOf="@+id/tvSubView_ip"
    android:layout_marginStart="20dp"
    android:hint="@string/etSubView_ip_hint"/>

<TextView 
    android:id="@+id/tvSubView_port"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/tvSubView_ip"
    android:paddingTop="20dp"
    android:text="@string/tvSubView_port"/>
<EditText 
    android:id="@+id/etSubView_port"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toEndOf="@+id/tvSubView_port"
    android:layout_below="@+id/etSubView_ip"
    android:layout_marginStart="7dp"
    android:hint="@string/etSubView_port_hint"/>

<TextView 
    android:id="@+id/tvSubView_clientID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/tvSubView_port"
    android:paddingTop="20dp"
    android:text="@string/tvSubView_clientID"/>
<EditText 
    android:id="@+id/etSubView_clientID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toEndOf="@+id/tvSubView_clientID"
    android:layout_below="@+id/etSubView_port"
    android:layout_marginStart="7dp"
    android:hint="@string/etSubView_clientID_hint"/>

<TextView 
    android:id="@+id/tvSubView_KATimer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/tvSubView_clientID"
    android:paddingTop="20dp"
    android:text="@string/tvSubView_KATimer"/>
<EditText 
    android:id="@+id/etSubView_KATimer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toEndOf="@+id/tvSubView_KATimer"
    android:layout_below="@+id/etSubView_clientID"
    android:layout_marginStart="7dp"
    android:hint="@string/etSubView_KATimer_hint"/>

<CheckBox
    android:id="@+id/cbSubView_CleanSession"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tvSubView_KATimer"
    android:layout_marginTop="20dp"
    android:gravity="center_vertical"
    android:checked="true"
    android:text="@string/cbSubView_CleanSession"/>

方法showConfigStubView

protected void showConfigStubView() {
    // TODO Auto-generated method stub
    this.rl_Root.setVisibility(View.GONE);
    viewStub = new ViewStub(getActivity());

    RelativeLayout relativeLayout = new RelativeLayout(getActivity());
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams 
            ((int) LayoutParams.WRAP_CONTENT, (int) LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.CENTER_HORIZONTAL);
    params.topMargin = 10;
    viewStub.setLayoutParams(params);
    //rl_Root.addView(viewStub);

    viewStub.setLayoutResource(R.layout.mqttconfig_subtree);
    relativeLayout.addView(viewStub);
    View inflated = viewStub.inflate();
}

1 个答案:

答案 0 :(得分:0)

请注意,viewStub应始终属于非null的ViewGroup或Layout。将RelativeLayout可见性设置为Gone会被视为没有viewStub应属于的viewGroup。我建议尝试以下方法:

protected void showConfigStubView() {
    // TODO Auto-generated method stub
    this.rg_ConfigChoice.setVisibility(View.GONE);
    this.btn_connect.setVisibility(View.GONE);
    this.viewStub = new ViewStub(getActivity());
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams 
            ((int) LayoutParams.WRAP_CONTENT, (int) LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.CENTER_HORIZONTAL);
    this.viewStub.setLayoutParams(params);
    this.rl_Root.addView(viewStub);
    this.viewStub.setLayoutResource(R.layout.mqttconfig_subtree);
    View inflated = viewStub.inflate();
}