2016-09-27 18 views
1

私は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" /> 

activity_choose_number1.xmlこれはこれは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" /> 
ある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は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をサブクラス化し、第1の合成Drawableを測定するために、そのonDraw()メソッドをオーバーライドしますテキスト幅とパディングを入力してからを翻訳してからsuperメソッドを実際に呼び出す前にドローをする。

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; 
    } 
} 

CheckedTextViewは、どのそれはcheckMarkDrawableを置く終了決定checkMarkGravity属性を持っていますが、それはいくつかの奇妙な理由のために、公共のではありませんので、我々はちょうど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自体についてはもっと詳しく述べました。スクリーンショットの円形の形状でさえ、私にはちょっと右にずれて見えます。ただの視覚的な好み。大事なことはありません。 –

関連する問題