繁体   English   中英

部分可点击的TextView和文本的不同文本颜色-Android

[英]Partially clickable TextView and different text colors for the text -Android

我无法在xml中对齐。 我做这个 我可以做这个

但我想这样做 我想这样做

我的xml代码是;

<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:orientation="horizontal"
            android:layout_marginTop="25dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:gravity="center">
            <TextView
                android:id="@+id/signup1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#733E3A"
                android:textSize="15dp"
                android:text="@string/have_account"
                fontPath="fonts/pfdindisplaypro-regular.ttf"
                tools:ignore="MissingPrefix"
                android:layout_gravity="center_horizontal" />
            <TextView
                android:id="@+id/txt_login_go_to_register"
                android:onClick="loginOnClickListener"
                android:clickable="true"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/signup1"
                android:textColor="@color/white"
                android:layout_marginLeft="7dp"
                android:textSize="15dp"
                android:text="@string/txt_register_title"
                fontPath="fonts/pfdindisplaypro-regular.ttf"
                tools:ignore="MissingPrefix"
                android:layout_gravity="center_horizontal" />
        </RelativeLayout>

我用过2 TextView。 其中之一是静态的,但第二个是动态的。 当用户单击第二个TextView时,片段将进入抵抗页面。

您可以使用Spannable实现

TextView布局。

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView"
    android:clickable="true"
    android:background="#BF7366" //Use your Background image or any other color
    android:padding="5dp"
    />

在你的activity.java中

TextView textView =(TextView) findViewById(R.id.textView);
textView.setMovementMethod(LinkMovementMethod.getInstance());

Spannable word = new SpannableString("Don`t have account yet? ");

word.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.text_color)), 0, word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

textView.setText(word);
final Spannable wordTwo = new SpannableString("Sign up here and join our exciting team.");

ClickableSpan myClickableSpan = new ClickableSpan()
{
    @Override
    public void onClick(View widget) {
       // There is the OnCLick. put your intent to Register class here
        widget.invalidate();
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        ds.setColor(Color.WHITE);
        ds.setUnderlineText(false);
    }
};
wordTwo.setSpan(myClickableSpan,0, wordTwo.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.append(wordTwo);

屏幕截图

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM