2016-09-27 258 views
1

我activity_choose_number1.xml如何将android中checkedTextView的复选框和文本居中?

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:popupTheme="@style/AppTheme.PopupOverlay" /> 

</android.support.design.widget.AppBarLayout> 

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

这是content_choose_number1.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:gravity="center_horizontal" 
    android:orientation="horizontal" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.abc.abc.ChooseNumber1" 
    tools:showIn="@layout/activity_choose_number1"> 

    <ListView 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:gravity="center_horizontal" 
     android:orientation="horizontal" 
     android:id="@+id/listViewNumberList" 
     android:choiceMode="singleChoice" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="0dp" /> 

</LinearLayout> 

这是secnumsinglechoice.xml

<?xml version="1.0" encoding="utf-8"?> 
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent" 
    android:textAppearance="?android:attr/textAppearanceListItemSmall" 
    android:checkMark="@null" 
    android:drawableStart="?android:attr/listChoiceIndicatorSingle" 
    tools:targetApi="ice_cream_sandwich" 
    android:textSize="25dp" 
    android:linksClickable="false" 
    android:checked="false" 
    android:paddingTop="20dp" 
    android:paddingBottom="20dp" /> 

secnumsinglechoice.xml是android.R.layout.simple_list_item_single_choice的复制粘贴,并进行了一些更改。

现在我需要的是中心对齐secnumusingchoice.xml的复选框和文本。我还需要复选框,留下文字的侧

事情我试过列表视图中的每一项 1)如果我做drawableLeft,它创建文本和复选框之间的空间,但不会使双方为中心 2)如果我添加android:textAlignment =“center”,那么它仍然中心对齐文本,但不是复选框。 3)我在这里尝试center text and checkmark of CheckedTextView,但我想在这里尝试更多的代码。

请帮助。谢谢。

+0

我不知道你是什么意思“我想与更多的代码来这里尝试”,但你尝试了'gravity'属性?例如,'android:gravity =“center_horizo​​ntal”'? –

+0

为什么不单独使用checkbox和textview?尝试使用'CheckedTextView'很难对齐复选框和文本.. –

+0

@MikeM。是的,尽管我仍然没有工作。 – user3705478

回答

2

这就是你想要

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="40dp" 
     android:background="@android:color/white" 
     android:gravity="center"> 

     <CheckBox 
      android:id="@+id/ckb" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:checked="true" /> 

     <TextView 
      android:id="@+id/tvEduName" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" 
      android:text="ABC" 
      android:textSize="17sp" /> 
    </LinearLayout> 

结果

enter image description here

+0

问题是显示给用户的选项数量是动态的。例如,对于某些用户,它可能是5条记录,对于某些用户可能是10条记录等。我无法对选项进行硬编码,我们也不知道我们将获得的选项数量。 – user3705478

+0

我认为这就是为什么你使用listview和动态数据的原因。 –

1

要正确地获得一切中心是什么,我们将继承CheckedTextView,并覆盖其onDraw()方法首先测量组合Drawable和文本宽度和填充,然后在调用super方法之前相应地翻译Canvas做平局。

import android.content.Context; 
import android.graphics.Canvas; 
import android.util.AttributeSet; 
import android.widget.CheckedTextView; 
import android.text.Layout; 

public class CenteredCheckedTextView extends CheckedTextView { 
    public CenteredCheckedTextView(Context context) { 
     this(context, null); 
    } 

    public CenteredCheckedTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     if (getCompoundDrawables()[0] == null) { 
      super.onDraw(canvas); 
     } 
     else { 
      // Left drawable and paddings width 
      final float dw = getCompoundDrawables()[0].getIntrinsicWidth() + 
          getPaddingLeft() + getCompoundDrawablePadding(); 
      // Text width 
      final float tw = getMaxLineMeasure(); 

      canvas.save(); 
      canvas.translate((getWidth() - (dw + tw))/2f, 0f); 
      super.onDraw(canvas); 
      canvas.restore(); 
     } 
    } 

    private float getMaxLineMeasure() { 
     final Layout layout = getLayout(); 
     final int lines = getLineCount(); 
     float m = 0, max = 0; 

     for (int i = 0; i < lines; ++i) { 
      m = getPaint().measureText(getText(), 
             layout.getLineStart(i), 
             layout.getLineEnd(i)); 
      if (m > max) { 
       max = m; 
      } 
     } 

     return max; 
    } 
} 

CheckedTextViewcheckMarkGravity属性决定在其结束它把checkMarkDrawable,但它不是公开的,一些奇怪的原因,所以我们只使用drawableLeft。例如:

<com.mycompany.myapp.CenteredCheckedTextView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingTop="20dp" 
    android:paddingBottom="20dp" 
    android:textSize="25dp" 
    android:drawableLeft="?android:attr/listChoiceIndicatorSingle" /> 

您可能需要在自定义类的翻译和/或宽度值略微拨弄得到一切的看上去中心给你排队,这取决于什么样的透明,内在填充Drawable


screenshot

+0

令人信服。我还没有尝试过这个解决方案。但是,当你说我需要稍微摆弄宽度值时,如果应用程序在平板电脑或其他屏幕尺寸中使用,对齐看起来会不是居中? – user3705478

+0

我并不是在谈论屏幕尺寸,但更多的是关于drawable本身可能在任何一边都有透明衬垫,这可能会使它看起来有点偏离中心。即使是截图中的圆形,也会让我看起来有点右移。只是视觉偏好,真的。没什么大不了 –