2015-01-13 85 views
0

我有一個包含一些看法和布頓,當按鈕被按下一個名爲showConfigStubView方法的RelativeLayout rl_root被調用,這是使佈局rl_root消失和viewStub應更換此ViewGroup中rl_rootViewStub不顯示?

在運行時,當方法showConfigStubView被調用時,viewGroup rl_root消失,並且viewStub不顯示。

的int 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(); 
} 

回答

0

需要注意的是,viewStub應該永遠屬於一個非空的ViewGroup或佈局。將您的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(); 
}