how can I make a separator line with "OR" in the middle in RelativeLayout in xml file of an activity? I'm trying to make something like " ------- OR --------" , with proper lines and "OR" in middle.
答案 0 :(得分:0)
尝试使用宽度为“match_parent”/“fill_parent”的TextView,并将其重力放在中心位置。
答案 1 :(得分:0)
经过一番研究后,我在这里找到了这个问题: Source
这应该就是你所需要的。本质是创建一个自定义可绘制类,它扩展 Drawable 并在其构造函数中获取文本。
如果您需要更多信息,我需要一些时间来完成您的自定义绘图。
编辑:这里是完整的来源
package com.example.XXX.customdividerdrawable;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
public class TextDividerDrawable extends Drawable {
private final String text;
private final Paint paint;
public TextDividerDrawable(String text, Paint paint) {
this.text = text;
this.paint = paint;
}
@Override
public void draw(Canvas canvas) {
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
int margin = 10;
canvas.drawLine(
margin, (canvas.getHeight() / 2) - (bounds.height() / 2),
canvas.getWidth() / 2 - margin,
(canvas.getHeight() / 2) - (bounds.height() / 2), paint
);
canvas.drawText(text, canvas.getWidth() / 2, canvas.getHeight() / 2, paint);
canvas.drawLine(
canvas.getWidth() / 2 + (margin + bounds.width()),
(canvas.getHeight() / 2) - (bounds.height() / 2),
canvas.getWidth() - margin,
(canvas.getHeight() / 2) - (bounds.height() / 2), paint
);
}
@Override
public void setAlpha(int alpha) {
paint.setAlpha(alpha);
}
@Override
public void setColorFilter(ColorFilter cf) {
paint.setColorFilter(cf);
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
}
不要将此作为背景放在您想要的每个视图中:
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(22f);
paint.setStyle(Paint.Style.FILL);
TextDividerDrawable textDividerDrawable = new TextDividerDrawable("OR", paint);
View view= findViewById(R.id.view);
view.setBackground(textDividerDrawable);
例如:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.XXX.customdividerdrawable.MainActivity">
<View
android:id="@+id/view"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</View>
</RelativeLayout>
答案 2 :(得分:0)
您可以尝试以下内容。
<LinearLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:background = "#ffffff"
android:gravity = "center"
android:orientation = "horizontal">
<View
android:layout_width = "75dp"
android:layout_height = "3dp"
android:background = "#000"/>
<TextView
android:id = "@+id/tv"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_gravity = "center"
android:layout_marginLeft = "5dp"
android:layout_marginRight = "5dp"
android:gravity = "center"
android:text = "or"
android:textSize = "24sp"/>
<View
android:layout_width = "75dp"
android:layout_height = "3dp"
android:background = "#000"/>
</LinearLayout>
希望它会对你有所帮助。