2015-07-21 101 views
6

我有創造與對齊到文本的開始按鈕CardView一個問題:CardView按鈕和文本對齊不

Screenshot of CardView with issue

我下面列出的材料尺寸設計填充和文本大小的規範。這是我正在使用的XML:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="8dp"> 

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

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:paddingBottom="16dp" 
      android:paddingLeft="16dp" 
      android:paddingRight="16dp" 
      android:paddingTop="24dp" 
      android:text="Title goes here" 
      android:textColor="@color/primary_text_default_material_light" 
      android:textSize="24sp" /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:paddingBottom="16dp" 
      android:paddingLeft="16dp" 
      android:paddingRight="16dp" 
      android:text="Subtitle here" 
      android:textSize="14sp" /> 

     <Button 
      style="@style/Base.Widget.AppCompat.Button.Borderless.Colored" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_margin="8dp" 
      android:padding="0dp" 
      android:text="Action 1" 
      android:textColor="@color/accent_material_light" /> 
    </LinearLayout> 
</android.support.v7.widget.CardView> 

從視覺上看,文本的開始與無邊界按鈕文本之間存在差距。

enter image description here

正如Material Design spec所示,按鈕,標題和副標題開始就要排隊:

Material design cardview showing aligned text and button

我要去哪裏錯了?

回答

2

您應該刪除了android:layout_margin和android:填充屬性和與Android取代: paddingLeft =「8dp」,如下所示。您還應該考慮使用android:paddingStart更好地支持從右到左的佈局,並使用android:paddingRight和android:paddingEnd更好地支持從右到左的對稱性。

<Button 
     style="@style/Base.Widget.AppCompat.Button.Borderless.Colored" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingLeft="8dp" 
     android:text="Action 1" 
     android:textColor="@color/accent_material_light" /> 
1

您需要設置按鈕的重力

android:gravity="start|center_vertical" 
+0

我只是嘗試這樣做,我注意到,如果按鈕上的文字很短的長度,則填充變成兩邊不平衡,導致在沒有垂直對齊關鍵線這 – user2560886

0

嘗試 的android:paddingLeft = 「0dp」

+0

不起作用了同樣的問題 – user2560886

+0

如果您刪除按鈕android:layout_margin =「8dp」,會發生什麼情況? –

+0

該按鈕變得太左對齊 – user2560886