2016-04-29 30 views
0

我有一個cardview只有一個textview。應該CardView總是首先包裝佈局?

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/card_view" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="1dp" 
    android:foreground="@drawable/selector_normal" 
    android:clickable="true" 
    card_view:cardCornerRadius="2dp"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <TextView 
      android:id="@+id/txt_command" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:drawablePadding="@dimen/padding_standard" 
      android:padding="@dimen/padding_standard" 
      android:textColor="@color/colorLightBlue" 
      android:textSize="@dimen/text_size_medium_small" /> 

    </LinearLayout> 
</android.support.v7.widget.CardView> 

顯然,LinearLayout除了包裝TextView之外沒有任何其他操作。但是,如果我刪除了LinearLayout,CardView只包裝了TextView,我的整個TextView就消失了。這是不允許的?

是否可以移除LinearLayout?

我想通過刪除冗餘佈局來減少佈局層次結構。

+1

那麼一個簡單的問題給你「你有沒有嘗試做呢?」 –

+0

@RakshitNawani OP提到他**做過**。但顯然他的TextView消失了。 –

+0

刪除'LinearLayout'後,您必須將'layout_width'更改爲'match_parent'或'wrap_content'並刪除現在已過時的'layout_weight'屬性 – AgileNinja

回答

2

不,這是沒有必要的擁有的LinearLayout上面比android.support.v7.widget.CardView

<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/card_view" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="1dp" 
    android:clickable="true" 
    android:foreground="@drawable/selector_normal" 
    card_view:cardCornerRadius="2dp"> 

    <TextView 
     android:id="@+id/txt_command" 
     android:layout_width="match_parent" //change here to match_parent 
     android:layout_height="wrap_content" 
     android:drawablePadding="@dimen/padding_standard" 
     android:padding="@dimen/padding_standard" 
     android:textColor="@color/colorLightBlue" 
     android:hint="Your Text Here" 
     android:textSize="@dimen/text_size_medium_small"/> 

</android.support.v7.widget.CardView> 
+0

謝謝!顯然我忘記了改變TextView layout_width =「0dp」,這使它消失。 – Elye

+0

不要認爲我的問題值得一個-1,但:(因爲這是一個有效的問題,其他人可能會面臨這個問題。您的答案有助於其他人注意到這個問題。所以我已upvote並勾選您的答案。 謝謝! – Elye

相關問題