首页 > 解决方案 > Android Studio:如何在一个按钮中设置标题和描述?

问题描述

我有ListView可以添加两个的地方TextView

但我需要向一个按钮添加NAME和。INFO你可以推荐我什么?

<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:layout_marginStart="7dp"
android:layout_marginTop="7.5dp"
android:layout_marginEnd="7dp"
android:layout_marginBottom="7.5dp"
android:background="@drawable/list_background"
android:elevation="5dp"
android:orientation="vertical"
android:paddingBottom="12dp">

<TextView
    android:id="@+id/subject_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="15dp"
    android:textAllCaps="true"
    android:textColor="@color/textColor"
    android:textSize="30sp"
    tools:text="Name" />

<TextView
    android:id="@+id/subject_info"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingStart="15dp"
    android:paddingEnd="5dp"
    android:textColor="@color/infoColor"
    android:textSize="16sp"
    tools:text="Info" />

例如我想要像这个按钮这样的按钮

标签: javaandroidlistviewbutton

解决方案


使用 Spanable 字符串。您可以设置大小和样式跨度以更改文本大小并使其变为粗体。

 String s= "Hello Everyone";
 SpannableString ss1=  new SpannableString(s);
 ss1.setSpan(new RelativeSizeSpan(2f), 0,5, 0); // set size
 StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
 ss1.setSpan(boldSpan, 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 TextView tv= (TextView) findViewById(R.id.textview);
 tv.setText(ss1);

输出如下 在此处输入图像描述


推荐阅读