android:如何在另一个内联显示文本

时间:2013-12-13 03:56:22

标签: android textview

是否有任何解决方案可以拥有2个这样的textview:

image

实际上我想在Text1行下面显示Text2行。例如,显示翻译Text1。

任何解决方案?

3 个答案:

答案 0 :(得分:1)

您可以使用setLineSpacing在TextView中设置足够大的间隙并将它们重叠,然后将其翻译,以便显示在另一个之间的行之间。

答案 1 :(得分:1)

试试这段代码。 您可以设置TextView的lineSpacingExtra属性,并可以使用相对布局来重叠它们。在此之后,您可以相应地设置文本视图的属性

In your xml define text view properties like this

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is Text 1. This is Text1. This is Text 1. This is Text1. This is Text 1. This is Text1"
        android:lineSpacingExtra="20dp"
        android:textColor="#5F4C0B"
        android:textSize="24sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="26dp"
        android:lineSpacingExtra="26dp"
        android:text="This is Text 2. This is Text2. This is Text2. This is Text2. This is Text2. This is Text2"
        android:textColor="#B40404"
        android:textSize="20sp" />

</RelativeLayout>

你会得到这样的输出

enter image description here

如果您觉得有用,请接受答案

答案 2 :(得分:0)

您可以动态创建此视图

在主活动中编写以下代码

    public class MainActivity extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            RelativeLayout rl = new RelativeLayout(this);
            RelativeLayout.LayoutParams text1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            text1.setMargins(0, 0, 0, 0);
            RelativeLayout.LayoutParams text2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            text2.setMargins(0, 26, 0, 0);

            LinesTextView tv1 = new LinesTextView(this, null);
            tv1.setText("This is Text 1. This is Text 1. This is Text 1. This is Text 1. This is Text 1. This is Text 1.");
            tv1.setLineSpacing(20.0f, 1.0f);
            tv1.setLayoutParams(text1);
            tv1.setTextSize(24.0f);
            tv1.setTextColor(getResources().getColor(android.R.color.darker_gray));

            LinesTextView tv2 = new LinesTextView(this, null);
            tv2.setText("This is Text 2. This is Text 2. This is Text 2. This is Text 2. This is Text 2. This is Text 2.");
            tv2.setLineSpacing(25.0f, 1.0f);
            tv2.setLayoutParams(text2);
            tv2.setTextSize(18.0f);
            tv2.setTextColor(getResources().getColor(android.R.color.black));
            rl.addView(tv1);
            rl.addView(tv2);

            this.setContentView(rl);
        }
    }

在LinesTextView类中编写以下代码

public class LinesTextView extends TextView
{
    private Rect mRect;
    private Paint mPaint;

    // we need this constructor for LayoutInflater
    public LinesTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        mRect = new Rect();
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(0x800000FF);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int count = getLineCount();
        Rect r = mRect;
        Paint paint = mPaint;

        for (int i = 0; i < count; i++) {
            int baseline = getLineBounds(i, r);

            canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
        }

        super.onDraw(canvas);
    }
} 

检查屏幕截图

enter image description here

相应地设置文本颜色,样式,边距和大小。