我们可以为单个textview设置两个引力

时间:2016-02-20 10:12:45

标签: android

我必须为单个TextView设置两个引力才有可能

类似这样,

  This line is example|
      Center Text     |
             I m right|

我必须在单个TextView中实现这一点,所以请不要建议我使用TextView1 CENTER重力和另一个具有RIGHT重力的TextView2声明两个textview。

是否可以使用Span或Html或任何其他属性。

2 个答案:

答案 0 :(得分:1)

使用Spannable解决问题

spannablecontent.setSpan(new AlignmentSpan.Standard( Layout.Alignment.ALIGN_OPPOSITE), 0, 15, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

对于Single TextView,我们可以将其指定为

左= ALIGN_NORMAL

中心= ALIGN_CENTER

右= ALIGN_OPPOSITE。

答案 1 :(得分:-1)

如果您只需要一个textview,那么使用HTML会为您提供解决方案。我在下面发布了它。

activity_main

 <LinearLayout 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"
        tools:context=".MainActivity"
        android:orientation="vertical" >
        <TextView
            android:id="@+id/html_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    </LinearLayout>

MainActivity.java

import android.os.Bundle;
import android.app.Activity;
import android.text.Html;
import android.widget.TextView;

public class MainActivity extends Activity {

    private final String htmlText = "<body><p>This line is example|  " +"<br>"+ 
            "&nbsp;&nbsp;&nbsp;&nbsp;Center Text&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|" +"<br>"+
            "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I am right|";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView htmlTextView = (TextView)findViewById(R.id.html_text);
        htmlTextView.setText(Html.fromHtml(htmlText));

    }


}

现在,您可以使用HTML

更改文本颜色或任何您喜欢的内容
相关问题