2013-12-13 78 views
0

有什麼解決辦法有2個TextView的是這樣的:我想顯示文本1線下方文本2線的android:如何顯示一個文本內嵌另一個

image

其實。例如顯示翻譯文本1。

任何解決方案?

+0

只要把它們放在一個下面。這是佈局的目的。您可以使用RelativeLayout或垂直LinearLayout輕鬆完成此操作。 –

+0

您是使用XML還是編程創建TextView? –

回答

1

您可以使用setLineSpacing來設置你的TextViews一個足夠大的差距,重疊它們,然後把它翻譯這樣一個在其他的字裏行間顯示出來。

+0

我想補充一些細節,你必須調整兩個TextViews邊緣,你會想用一個佈局,使您可以重疊的意見,以及查看背景設置爲透明的,但在整個這是在可能是實現效果的最簡單方法之一。 – TheIT

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

輸出接受的答案,如果你發現它有用

+0

爲什麼有人會嘗試你的代碼?你提出什麼解決方案來解決特定問題?包括更多的細節來獲得投票和讚賞。 #提示 –

+1

感謝您的建議@Paresh。下次我會記住它。 –

+0

乾杯!快照看起來不錯。 –

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

設置文本顏色,樣式,邊緣和大小相應。

相關問題