2015-07-21 98 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