我正在使用 MaterialButtonToggleGroup 创建选择器按钮。我想更改 MaterialButton 按钮的背景颜色。它的默认颜色是浅蓝色,我想将其更改为浅绿色。目前,我正在使用可绘制扇区来更改背景颜色,但它不起作用。
这是我的布局,
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/toggleContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:checkedButton="@id/btnOutline"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:visibility="visible"
app:layout_constraintRight_toRightOf="parent"
app:singleSelection="true">
<com.google.android.material.button.MaterialButton
android:id="@+id/btnOutline"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_selector"
android:text="@string/outline"
android:textAllCaps="false" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btnMain"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:background="@drawable/button_selector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/main"
android:textAllCaps="false" />
</com.google.android.material.button.MaterialButtonToggleGroup>
Run Code Online (Sandbox Code Playgroud)
这是我的可绘制文件“button_selector”,
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/selector_color"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
这是“selector_color”文件,
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#93cdcb"/>
</shape>
Run Code Online (Sandbox Code Playgroud) 我无法更改所选 MaterialButton 的背景。
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:singleSelection="true"
app:selectionRequired="true"
app:checkedButton="@+id/toggleButtonFolder">
<com.google.android.material.button.MaterialButton
android:id="@+id/toggleButtonFolder"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:shapeAppearance="@style/ToggleButton.Rounded"
android:text="Folders" />
<com.google.android.material.button.MaterialButton
android:id="@+id/toggleButtonRecent"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:shapeAppearance="@style/ToggleButton.Rounded"
android:text="Recent" />
</com.google.android.material.button.MaterialButtonToggleGroup>
Run Code Online (Sandbox Code Playgroud)
样式 ToggleButton.Rounded:
<style name="ToggleButton.Rounded" parent="ShapeAppearance.MaterialComponents.SmallComponent">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">16dp</item>
</style>
Run Code Online (Sandbox Code Playgroud)
代码的结果是:
我想达到的目标:
android android-button android-togglebutton material-components-android
如果我尝试使用layout_marginStart或layout_marginEnd添加边距,但对 UI 没有影响。当我将它们添加为MaterialButtonToggleGroup的子项时,我不确定为什么layout_marginStart、layout_marginEnd不能与MaterialButton一起使用
<com.google.android.material.button.MaterialButtonToggleGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/ten_dp"
app:singleSelection="true">
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/twentY"
app:icon="@drawable/ic_directions_walk_black_24dp" />
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/ten_dp"
app:icon="@drawable/ic_directions_car_black_24dp" />
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/ten_dp"
app:icon="@drawable/ic_directions_bus_black_24dp" />
</com.google.android.material.button.MaterialButtonToggleGroup>
Run Code Online (Sandbox Code Playgroud) android android-button android-togglebutton material-design material-components-android
我正在尝试创建一个 MaterialButtonToggleGroup,其中包含材质按钮,并且我想使用自定义样式来做到这一点。
我能够使用 xml 创建它,但是,我需要以编程方式实现相同的结果
这是我对 xml 所做的事情(我使用 LinearLayout 作为根视图):
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/group_item_size"
style="@style/ButtonsToggleGroupStyle"
>
<com.google.android.material.button.MaterialButton
style="@style/ButtonStyle"
android:text="Small"
/>
<com.google.android.material.button.MaterialButton
style="@style/ButtonStyle"
android:text="Large"
/>
<com.google.android.material.button.MaterialButton
style="@style/ButtonStyle"
android:text="Family"
/>
</com.google.android.material.button.MaterialButtonToggleGroup>
Run Code Online (Sandbox Code Playgroud)
以及样式的代码:
<style name="ButtonStyle" parent="Widget.MaterialComponents.Button.UnelevatedButton">
<item name="cornerRadius">@dimen/margin_20dp</item>
<item name="android:padding">@dimen/margin_8dp</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">match_parent</item>
<item name="android:layout_weight">1</item>
<item name="android:textSize">@dimen/text_size_14sp</item>
<item name="android:textAllCaps">false</item>
<item name="android:textColor">@drawable/drawable_button_toggle_group_text_selector</item>
<item name="android:backgroundTint">@drawable/drawable_button_toggle_group_selector</item>
</style>
<style name="ButtonsToggleGroupStyle">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_gravity">center</item>
<item name="android:orientation">horizontal</item>
<item name="android:layout_marginStart">@dimen/margin_32dp</item>
<item name="android:layout_marginTop">@dimen/margin_12dp</item>
<item name="android:layout_marginEnd">@dimen/margin_32dp</item>
</style>
Run Code Online (Sandbox Code Playgroud)
在此先感谢您的帮助
android android-layout android-button android-togglebutton material-components-android
我使用的MaterialButtonToggleGroup是单选(一次只选中一个按钮)。如何检查是否没有选中任何按钮?
toggleGroup?.addOnButtonCheckedListener { group, checkedId, isChecked ->
if (isChecked) {
when (checkedId) {
R.id.first_materialButton -> {
// do something when selected
}
R.id.second_materialButton -> {
// do something when selected
}
}
}
}
Run Code Online (Sandbox Code Playgroud) android android-button android-togglebutton material-components material-components-android
嗨,我正在尝试通过xml 更改切换按钮的文本颜色.
我已经引用了链接,但它只更改了切换按钮的背景颜色,而不是其文本.
我试过这种方法:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#ffffff" />
<item android:state_checked="false" android:color="#000000" />
</selector>
Run Code Online (Sandbox Code Playgroud)
但只有背景在变化.
注意:我不想在代码中执行此操作,因为有21个切换按钮,并且每个按钮的设置监听器都不好.
android toggle togglebutton android-button android-togglebutton
我想制作一个自定义切换按钮,例如当我点击按钮时,星形会亮起,当我再次点击时,星形会关闭.我尝试了一些代码,但是他们将按钮更改为图像,当我点击它时,图像会发生变化.我的意思是,没有按钮,它只是一个图像.
我希望图像在按钮内部,就像默认切换按钮一样,我只需要更改其中的图像.
我怎么能这样做?
谢谢
这是我的代码
@Override
public void onBindViewHolder(final RecyclerViewHolder holder, int position) {
final Songs songs = arrayList.get(position);
holder.favorite.setChecked(songs.getFavorite()); // this line makes me confusing.
holder.favorite.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
int r = db.update_favorite(b,songs.getTitle(),songs.getArtist());
compoundButton.setChecked(b);
}
});
}
Run Code Online (Sandbox Code Playgroud)
这里的问题是我是否包含此代码
holder.favorite.setChecked(songs.getFavorite());
到onBindViewHolder,当我更改切换按钮的状态并向下滚动然后向上滚动时,切换按钮的状态将自身更改为原始状态。澄清一下,当切换按钮状态从 false 变为 true 并且用户向下和向上滚动时,切换按钮会自动变为原始状态,即为 false。
即使用户向下滚动,如何使切换按钮保持状态变化?请帮我。