2017-04-18 45 views
0

我有LinearLayout裏面的DrawerLayout根據layout_weight呈現抽屜項目和視圖。我試圖通過設置其高度爲0或以View.Visibility - GONE屬性以編程方式隱藏該視圖。DrawerLayout的內部視圖佈局總是返回FrameLayout

試過

  • 通過設定其公開程度爲GONE它充當INVISIBLE

  • 通過獲得其的LayoutParams並設置其高度爲0。雖然它作爲作用INVISIBLE

ParentLayout:mother.xml

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<android.support.v4.widget.DrawerLayout 
    android:id="@+id/activityMotherDrawerLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true"> 

    <include layout="@layout/activity_mother" /> 

    <include layout="@layout/motherdrawer" /> 

    </android.support.v4.widget.DrawerLayout> 

佈局:motherdrawer.xml

<?xml version="1.0" encoding="utf-8"?> 
<com.app.ui.view.ScrimInsetsFrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawerRootLayout" 
    android:layout_width="@dimen/activity_mother_drawer_width" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:background="@color/background_application" 
    android:fitsSystemWindows="false" 
    android:orientation="vertical"> 

<RelativeLayout 
    android:id="@+id/activityMotherDrawerInnerLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:padding="@dimen/activity_mother_drawer_layout_padding"> 

    <LinearLayout 
     android:id="@+id/activityMotherDrawerInnerInnerLayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 


     <LinearLayout 
      android:id="@+id/activityMotherDrawerMyAttendanceLayout" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_gravity="center" 
      android:layout_weight="1" 
      android:clickable="true" 
      android:orientation="vertical"> 

      <ImageView 
       android:id="@+id/activityMotherDrawerMyAttendanceIv" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_centerInParent="true" 
       android:layout_weight="1.5" 
       android:padding="@dimen/activity_mother_drawer_image_padding" 
       android:scaleType="centerInside" 
       android:src="@drawable/ic_drawer_attendance" /> 

      <android.support.v7.widget.AppCompatTextView 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_below="@id/activityMotherDrawerMyAttendanceIv" 
       android:layout_centerHorizontal="true" 
       android:layout_weight="1" 
       android:gravity="top|center" 
       android:text="@string/activity_mother_drawer_attendance" 
       android:textColor="@color/colorGray" 
       android:textSize="@dimen/activity_mother_drawer_text_size" /> 

     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/activityMotherDrawerDashboardLayout" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_gravity="center" 
      android:layout_weight="1" 
      android:clickable="true" 
      android:orientation="vertical"> 

      <ImageView 
       android:id="@+id/activityMotherDrawerDashboardIv" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_centerInParent="true" 
       android:layout_weight="1.5" 
       android:padding="@dimen/activity_mother_drawer_image_padding" 
       android:scaleType="centerInside" 
       android:src="@drawable/ic_drawer_dashboard" /> 

      <android.support.v7.widget.AppCompatTextView 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_below="@id/activityMotherDrawerDashboardIv" 
       android:layout_centerHorizontal="true" 
       android:layout_weight="1" 
       android:gravity="top|center" 
       android:text="@string/activity_mother_drawer_dashboard" 
       android:textColor="@color/colorGray" 
       android:textSize="@dimen/activity_mother_drawer_text_size" /> 

     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/activityMotherDrawerSettingsLayout" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_gravity="center" 
      android:layout_weight="1" 
      android:clickable="true" 
      android:orientation="vertical"> 

      <ImageView 
       android:id="@+id/activityMotherDrawerSettingsIv" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_centerInParent="true" 
       android:layout_weight="1.5" 
       android:padding="@dimen/activity_mother_drawer_image_padding" 
       android:scaleType="centerInside" 
       android:src="@drawable/ic_drawer_settings" /> 

      <android.support.v7.widget.AppCompatTextView 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_below="@id/activityMotherDrawerSettingsIv" 
       android:layout_centerHorizontal="true" 
       android:layout_weight="1" 
       android:gravity="top|center" 
       android:text="@string/activity_mother_drawer_setting" 
       android:textColor="@color/colorGray" 
       android:textSize="@dimen/activity_mother_drawer_text_size" /> 

     </LinearLayout> 
    </LinearLayout> 

    </RelativeLayout> 
</com.app.ui.view.ScrimInsetsFrameLayout>` 

每當我試着用來獲取layoutParam getLayoutPa rams它返回FrameLayout其中它的父視圖是LinearLayout。

使用Java:

mDashboardLayout = (LinearLayout)mDrawerInnerRootLayout.findViewById(R.id.activityMotherDrawerDashboardLayout); 
LinearLayout.LayoutParams mParams = (LinearLayout.LayoutParams) mDashboardLayout.getLayoutParams();//returns FrameLayout instead where it should return LinearLayout 

mDashboardLayout.setVisibility(View.GONE); //it acts as INVISIBILITY 

誰能開導我,爲什麼它返回,而不是 的LinearLayout或者的FrameLayout爲什麼它的可見性變爲不可見,而不是走了嗎?

回答

0

儘管更改視圖 - 可見性爲GONEINVISIBLE仍然視圖始終是隱形的。它確實接受onClick事件。根據SO,我想徹底刪除這個觀點。我能夠使用以下方法實現它。

我正在通過編程方式將View添加到ParentView層次結構中,並刷新整個佈局。

View inflatedDashboardView; 
    inflatedDashboardView = mInflater.inflate(R.layout.layout_drawer_dashboard, mDrawerInnerRootLayout, false); 
    mDashboardLayout = (LinearLayout) inflatedDashboardView.findViewById(R.id.activityMotherDrawerDashboardLayout); 
    mDrawerInnerRootLayout.addView(inflatedDashboardView); 
    mDrawerInnerRootLayout.forceLayout(); 

仍然我不明白爲什麼內部ViewView佈局裏面的DrawerLayout總是返回FrameLayout。