我有一个要重新创建的按钮,它看起来像这样:
我已经通过使用普通的Button
和drawable
作为背景来完成了其中的一部分。但是,如何在按钮内重新创建文本布局?我尝试将TextView
放在ConstraintLayout
内的Button
内,但是没有用。
如何创建可以通过编程方式更改的布局?
答案 0 :(得分:1)
要设置字符串样式,可以使用Spannable
。它非常强大,可以让您做自己想做的事。
例如,使K带有上标:
SpannableStringBuilder cs = new SpannableStringBuilder("K19");
cs.setSpan(new SuperscriptSpan(), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
对于较小的文本,您可以使用new RelativeSizeSpan(0.5f)
,也可以使用新行\n
使文本变为多行。
然后只需将Spannable设置为按钮的文本即可。
Here是更全面的指南。
答案 1 :(得分:1)
您可以创建自定义视图;设置文本元素在布局内的位置,配置背景以使其在pressed|selected|enabled|disabled
时具有多种状态,最后将onClickListener
附加到视图;
对于自定义视图,我使用了它:
public class WrapperPeriodicElement extends ConstraintLayout {
private TextView textTop;
private TextView textMiddle;
private TextView textBottom;
private String strTextTop;
private String strTextMiddle;
private String strTextBottom;
public WrapperPeriodicElement(Context context) {
this(context, null);
}
public WrapperPeriodicElement(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.periodic_element, this);
this.textTop = view.findViewById(R.id.text_top);
this.textMiddle = view.findViewById(R.id.text_middle);
this.textBottom = view.findViewById(R.id.text_bottom);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.WrapperPeriodicElement, 0, 0);
try {
strTextTop = array.getString(R.styleable.WrapperPeriodicElement_textTop);
strTextMiddle = array.getString(R.styleable.WrapperPeriodicElement_textMiddle);
strTextBottom = array.getString(R.styleable.WrapperPeriodicElement_textBottom);
} finally {
array.recycle();
}
textTop.setText(strTextTop);
textMiddle.setText(strTextMiddle);
textBottom.setText(strTextBottom);
}
public String getTextTop() {
return strTextTop;
}
public String getTextMiddle() {
return strTextMiddle;
}
public String getTextBottom() {
return strTextBottom;
}
public void setTextTop(String text) {
this.strTextTop = text;
textTop.setText(strTextTop);
invalidate();
requestLayout();
}
public void setTextMiddle(String text) {
this.strTextMiddle = text;
textMiddle.setText(strTextMiddle);
invalidate();
requestLayout();
}
public void setTextBottom(String text) {
this.strTextBottom = text;
textBottom.setText(strTextBottom);
invalidate();
requestLayout();
}
}
此元素的自定义视图:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="72dp"
android:layout_height="wrap_content"
android:background="@drawable/periodic_element_background"
android:foreground="?android:attr/selectableItemBackground">
<TextView
android:id="@+id/text_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginTop="4dp"
android:textAppearance="?android:textAppearanceSmall"
android:textColor="@color/colorAccent"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="019" />
<TextView
android:id="@+id/text_middle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:textAppearance="?android:textAppearanceLarge"
android:textColor="@color/colorAccent"
android:textSize="48sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Kg" />
<TextView
android:id="@+id/text_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="@color/colorAccent"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_middle"
tools:text="Potassium" />
</android.support.constraint.ConstraintLayout>
periodic_element
的背景:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/periodic_element_disabled" android:state_enabled="false" />
<item android:drawable="@drawable/periodic_element_pressed" android:state_enabled="true" android:state_pressed="true" />
<item android:drawable="@drawable/periodic_element_pressed" android:state_enabled="true" android:state_focused="true" />
<item android:drawable="@drawable/periodic_element_enabled" android:state_enabled="true" />
</selector>
在我的示例中,每个项目内部只有一种颜色,这里是periodic_element_disabled.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/periodic_element_disabled"/>
</shape>
main_activity.xml中的代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="horizontal"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="Are you made of Cooper and Tellurium?\nBecause you're"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.example.joao.periodicproject.customview.WrapperPeriodicElement
android:id="@+id/element_cooper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:textBottom="Cooper"
app:textMiddle="Cu" />
<com.example.joao.periodicproject.customview.WrapperPeriodicElement
android:id="@+id/element_telurium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toEndOf="@+id/element_cooper"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:textBottom="Tellurium"
app:textMiddle="Te"
app:textTop="52" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:text="I was going to make a joke about sodium, but..."
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/element_cooper" />
<com.example.joao.periodicproject.customview.WrapperPeriodicElement
android:id="@+id/element_sodium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
</android.support.constraint.ConstraintLayout>
最后是MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WrapperPeriodicElement cooper = findViewById(R.id.element_cooper);
cooper.setTextTop("29");
cooper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, cooper.getTextBottom(), Toast.LENGTH_LONG).show();
}
});
final WrapperPeriodicElement sodium = findViewById(R.id.element_sodium);
sodium.setTextTop("11");
sodium.setTextMiddle("Na");
sodium.setTextBottom("Sodium");
sodium.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, sodium.getTextBottom(), Toast.LENGTH_LONG).show();
}
});
}
}
结果在这里:
;
您可以在github
上检查代码答案 2 :(得分:0)
请勿使用按钮,因为这仅允许您使用一个文本。您可以通过创建 RelativeLayout 并向其中添加多个 TextViews ,然后为该布局创建一个点击侦听器,来实现所需的效果。通过将clickListener设置为按钮,几乎可以使任何按钮成为按钮。更改视图的背景以根据需要设置其样式。