【问题标题】:How to center the checkbox and text of checkedTextView in android?如何在android中将checkedTextView的复选框和文本居中?
【发布时间】:2017-02-04 12:50:11
【问题描述】:

我有activity_choose_number1.xml

<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,但我想在这里尝试更多代码。

请帮忙。谢谢。

【问题讨论】:

  • 为什么不分别使用checkboxtextview?尝试使用CheckedTextView 很难对齐复选框和文本..
  • @MikeM。是的,我试过了,还是不行。
  • @ roshanshan如果我单独使用我如何在选择相应的复选框时选择文本值?你能提供一些参考链接吗?谢谢。
  • @MikeM。在这个问题中,您会看到右侧的刻度线。它不像文本那样居中对齐。我希望我的文本和刻度线都居中对齐。
  • @MikeM。您能否提供代码作为答案?

标签: android xml android-layout checkbox checkedtextview


【解决方案1】:

这就是你想要的

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

结果

【讨论】:

  • 问题是向用户显示的选项数量是动态的。例如,对于某些用户可能是 5 条记录,对于某些用户可能是 10 条记录等。我无法对选项进行硬编码,而且我们不知道我们将获得的选项数量。
  • 我认为这就是您使用动态数据列表视图的原因。
【解决方案2】:

为了使所有内容正确居中,我们将继承 CheckedTextView,并覆盖其 onDraw() 方法,以首先测量组合的 Drawable 和文本宽度和填充,然后在调用之前相应地转换 Canvas 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 有一个checkMarkGravity 属性,用于确定将checkMark Drawable 放置在哪一端,但由于某些奇怪的原因,它不是公开的,所以我们只使用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 具有哪种透明的内在填充。


【讨论】:

  • 令人信服。我还没有尝试过解决方案。但是当你说我需要稍微调整宽度值时,如果应用程序用于平板电脑或其他屏幕尺寸,对齐方式会不会看起来不居中?
  • 我不是在谈论屏幕尺寸,而是更多关于可绘制对象本身可能在两侧都有透明填充,这可能使它看起来有点偏离中心。即使是屏幕截图中的圆形也让我看起来有点右移。只是视觉偏好,真的。没什么大不了的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-05
  • 1970-01-01
  • 2014-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多