Android 在 MaterialButtonToggleGroup 中添加带有多行文本的按钮不起作用
Android Adding a button with multiline text inside MaterialButtonToggleGroup doesn't work
我试图在 MaterialButtonToggleGroup 中显示一个多行按钮,但它总是在换行处显示 ...
但它在 MaterialButtonToggleGroup
之外工作正常我试过
- 使用
\n
和
- 使用
<br/>
HTML 标签和 Html.form 方法
如下图所示,第一个按钮位于 MaterialButtonToggleGroup 之外,但第二个按钮位于其中。
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
</data>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".conversation.ui.conversations.AddNewConversationFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_margin="20dp"
android:background="@drawable/register_field_background"
android:orientation="vertical"
android:padding="10dp">
<ImageView
android:id="@+id/img_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_baseline_close_24"
android:tint="@android:color/darker_gray"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription" />
<com.google.android.material.button.MaterialButton
android:id="@+id/add_anyone"
android:singleLine="false"
android:maxLines="2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@color/gray100"
android:text="This is Multi Line Text Line2"
android:textAllCaps="false"
android:textColor="@color/gray700"
app:iconTint="@color/gray700"
/>
<com.google.android.material.button.MaterialButtonToggleGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginBottom="10dp"
android:orientation="vertical"
app:checkedButton="@+id/button_add_native"
app:layout_constraintBottom_toTopOf="@+id/tv_select_address"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:selectionRequired="true"
app:singleSelection="true">
<com.google.android.material.button.MaterialButton
android:id="@+id/add_anyone"
android:singleLine="false"
android:maxLines="2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@color/gray100"
android:text="This is Multi Line Text Line2"
android:textAllCaps="false"
android:textColor="@color/gray700"
app:iconTint="@color/gray700"
/>
</com.google.android.material.button.MaterialButtonToggleGroup>
<TextView
android:id="@+id/textView"
style="@style/bottomSheetDialogHeader"
android:layout_marginStart="16dp"
android:letterSpacing="0.1"
android:text="@string/add_conversation_dialog_title"
android:textAllCaps="true"
app:layout_constraintBottom_toBottomOf="@+id/img_close"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/img_close" />
<com.google.android.material.button.MaterialButton
android:id="@+id/tv_select_address"
style="@style/LinguisticMainLayoutOrangeButton"
android:paddingBottom="10dp"
android:text="@string/start_chatting"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>
</layout>
这是预期的result.You可以检查下面多行被覆盖的源代码。
您可以通过编程方式进行设置。类似于:
val button : MaterialButton = findViewById(R.id.add_anyone)
button.maxLines = 2
源代码:
private void setupButtonChild(@NonNull MaterialButton buttonChild) {
buttonChild.setMaxLines(1);
buttonChild.setEllipsize(TruncateAt.END);
buttonChild.setCheckable(true);
备选技巧供日后参考:
- 提供您自己的自定义 class,我们将其命名为 CustomMaterialButtonToggleGroup,它扩展了 MaterialButtonToogleGroup
示例:
public class CustomMaterialButtonToggleGroup extends MaterialButtonToggleGroup {
public CustomMaterialButtonToggleGroup(@NonNull Context context) {
super(context);
}
public CustomMaterialButtonToggleGroup(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public CustomMaterialButtonToggleGroup(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
super.addView(child, index, params);
if(child instanceof MaterialButton)
{
((MaterialButton) child).setMaxLines(2);
}
}
这样我们通过代码强制 MaterialToggleGroup 的子视图实现 maxLines!
最后第二个奇怪的部分是:
- 转到您定义按钮的 Activity/Fragment...并通过代码进行设置!
示例:
SampleButton.setText("Hey\nI'm multiline"); // notice the escape character of newline-> \n
然后像往常一样在 strings.xml 中提取它!
我试图在 MaterialButtonToggleGroup 中显示一个多行按钮,但它总是在换行处显示 ...
但它在 MaterialButtonToggleGroup
之外工作正常我试过
- 使用
\n
和
- 使用
<br/>
HTML 标签和 Html.form 方法
如下图所示,第一个按钮位于 MaterialButtonToggleGroup 之外,但第二个按钮位于其中。
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
</data>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".conversation.ui.conversations.AddNewConversationFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_margin="20dp"
android:background="@drawable/register_field_background"
android:orientation="vertical"
android:padding="10dp">
<ImageView
android:id="@+id/img_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_baseline_close_24"
android:tint="@android:color/darker_gray"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription" />
<com.google.android.material.button.MaterialButton
android:id="@+id/add_anyone"
android:singleLine="false"
android:maxLines="2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@color/gray100"
android:text="This is Multi Line Text Line2"
android:textAllCaps="false"
android:textColor="@color/gray700"
app:iconTint="@color/gray700"
/>
<com.google.android.material.button.MaterialButtonToggleGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginBottom="10dp"
android:orientation="vertical"
app:checkedButton="@+id/button_add_native"
app:layout_constraintBottom_toTopOf="@+id/tv_select_address"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:selectionRequired="true"
app:singleSelection="true">
<com.google.android.material.button.MaterialButton
android:id="@+id/add_anyone"
android:singleLine="false"
android:maxLines="2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@color/gray100"
android:text="This is Multi Line Text Line2"
android:textAllCaps="false"
android:textColor="@color/gray700"
app:iconTint="@color/gray700"
/>
</com.google.android.material.button.MaterialButtonToggleGroup>
<TextView
android:id="@+id/textView"
style="@style/bottomSheetDialogHeader"
android:layout_marginStart="16dp"
android:letterSpacing="0.1"
android:text="@string/add_conversation_dialog_title"
android:textAllCaps="true"
app:layout_constraintBottom_toBottomOf="@+id/img_close"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/img_close" />
<com.google.android.material.button.MaterialButton
android:id="@+id/tv_select_address"
style="@style/LinguisticMainLayoutOrangeButton"
android:paddingBottom="10dp"
android:text="@string/start_chatting"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>
</layout>
这是预期的result.You可以检查下面多行被覆盖的源代码。
您可以通过编程方式进行设置。类似于:
val button : MaterialButton = findViewById(R.id.add_anyone)
button.maxLines = 2
源代码:
private void setupButtonChild(@NonNull MaterialButton buttonChild) {
buttonChild.setMaxLines(1);
buttonChild.setEllipsize(TruncateAt.END);
buttonChild.setCheckable(true);
备选技巧供日后参考:
- 提供您自己的自定义 class,我们将其命名为 CustomMaterialButtonToggleGroup,它扩展了 MaterialButtonToogleGroup
示例:
public class CustomMaterialButtonToggleGroup extends MaterialButtonToggleGroup {
public CustomMaterialButtonToggleGroup(@NonNull Context context) {
super(context);
}
public CustomMaterialButtonToggleGroup(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public CustomMaterialButtonToggleGroup(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
super.addView(child, index, params);
if(child instanceof MaterialButton)
{
((MaterialButton) child).setMaxLines(2);
}
}
这样我们通过代码强制 MaterialToggleGroup 的子视图实现 maxLines!
最后第二个奇怪的部分是:
- 转到您定义按钮的 Activity/Fragment...并通过代码进行设置!
示例:
SampleButton.setText("Hey\nI'm multiline"); // notice the escape character of newline-> \n
然后像往常一样在 strings.xml 中提取它!