Android:中间有文本的水平线 我想知道如何用中间的文字来做这个水平线,看这个截图:

Android:中间有文本的水平线 我想知道如何用中间的文字来做这个水平线,看这个截图:,android,Android,有人有办法在安卓系统上做到这一点吗?我发现如何做水平线,但从来没有文字 谢谢 在您的xml中 android:layout_width="fill_parent" android:layout_height="2dip" android:background="#FF00FF00" /> 或者,在代码中- View ruler = new View(myContext); ruler.setBackgroundColor(0xFF00FF00); theParent.addView(ru

有人有办法在安卓系统上做到这一点吗?我发现如何做水平线,但从来没有文字


谢谢

在您的xml中

android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#FF00FF00" />
或者,在代码中-

View ruler = new View(myContext); ruler.setBackgroundColor(0xFF00FF00);
theParent.addView(ruler,
new ViewGroup.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 2));

只需更改颜色以匹配图像上的颜色即可。我还建议您使用渐变作为这些虚拟视图的背景,如果您花一点时间,它看起来比屏幕截图要好得多

 <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true">

        <TextView
            android:id="@+id/tvText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:text="lala"
            android:textColor="#FFFFFF"/>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="16dp"
            android:layout_toLeftOf="@id/tvText"
            android:background="#FF0000"
            />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_centerVertical="true"
            android:layout_marginRight="16dp"
            android:layout_toRightOf="@id/tvText"
            android:background="#FF0000"
            />

    </RelativeLayout>

试试这个

相应地设置视图的宽度

 <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp">


            <View
            android:id="@+id/topDivider"
            android:layout_width="50dp"
            android:layout_height="1dp"
            android:layout_below="@id/internal"
            android:background="@color/bright_blue"/>

            <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Middle text here"
            android:layout_gravity="center_horizontal"
            android:id="@+id/lv_shopName" />


            <View
            android:id="@+id/topDivider"
            android:layout_width="50dp"
            android:layout_height="1dp"
            android:layout_below="@id/internal"
            android:background="@color/bright_blue"/>


            </LinearLayout>


您可以使用一个视图显示行,一个文本视图显示文本。只需在TextView的XML中添加android:layout\u centerHorizontal=“true”

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_margin="12dp"
            android:background="@android:color/black" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="2dp"
            android:background="@android:color/white"
            android:layout_centerHorizontal="true"
            android:text="or" />
    </RelativeLayout>


这不是我的问题,我知道怎么做。我想在这行的中间添加文本。Thx.因此,制作一张图片并将其置于textview最简单解决方案的背景下。制作透明的textview Dude这是一张静态图片,你可以这样做,也可以按照我的方式来做我发布的图片!工作完美;)我不能添加+1对不起,但对我来说一切都好@Asfi你很快就能做到,我想你只需要15分就可以+1篇帖子:)没问题。如果有人出错,不要忘记在RelativeLayout中声明名称空间:
xmlns:android=”http://schemas.android.com/apk/res/android“
public class CenterLineTextView extends android.support.v7.widget.AppCompatTextView {

private final Rect mBounds = new Rect();
private final Paint mPaint = new Paint();
private int mPadding;
private int mStroke;

public CenterLineTextView(Context context) {
    super(context);
    init();
}

public CenterLineTextView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    init();
}

public CenterLineTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

private void init() {
    if (isInEditMode()) {
        return;
    }
    setGravity(Gravity.CENTER);
    mStroke = getContext().getResources().getDimensionPixelSize(R.dimen.divider);
    mPadding = getContext().getResources().getDimensionPixelSize(R.dimen.login_or_padding);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    mPaint.setStrokeWidth(mStroke);
    mPaint.setColor(getPaint().getColor());
    getPaint().getTextBounds(getText().toString(), 0, getText().length(), mBounds);
    canvas.drawLine(0, getHeight() / 2, (getWidth() - mBounds.right) / 2 - mPadding, getHeight() / 2, mPaint);
    canvas.drawLine(mPadding + (getWidth() + mBounds.right) / 2, getHeight() / 2, getWidth(), getHeight() / 2, mPaint);
}
}
<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_margin="12dp"
            android:background="@android:color/black" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="2dp"
            android:background="@android:color/white"
            android:layout_centerHorizontal="true"
            android:text="or" />
    </RelativeLayout>