gpt4 book ai didi

android - 如何在一个按钮中放置多个文本?

转载 作者:行者123 更新时间:2023-11-29 18:46:50 27 4
gpt4 key购买 nike

我有一个我想重新创建的按钮,它看起来像这样:

enter image description here

我已经通过使用一个普通的 Button 和一个 drawable 作为背景完成了它的一部分。但是我怎样才能重新创建按钮内的文本布局呢?我尝试将 TextView 放在 Button 内的 ConstraintLayout 中,但它不起作用。

如何创建也可以通过编程方式更改的布局?

最佳答案

您可以创建自定义 View ;设置布局内的文本元素定位,配置背景以在 pressed|selected|enabled|disabled 时表现出多种状态,最后将 onClickListener 附加到 View ;

对于自定义 View ,我使用了这个:

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();
}
}

此元素的自定义 View :

<?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();
}
});

}
}

这里是结果:

result ;

您可以查看 github 上的代码

关于android - 如何在一个按钮中放置多个文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51543150/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com