gpt4 book ai didi

android - 如何在android中改变开关的形状

转载 作者:太空狗 更新时间:2023-10-29 16:30:27 26 4
gpt4 key购买 nike

我使用开关创建了一个布局文件。现在我想更改其选择器的形状。我还想删除此开关上的文本。

目前它的形状是矩形。我想把它改成圆形。

This is my requirement

这是我的布局文件,

<?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:id="@+id/activity_remind_me"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.pvt.RemindMeActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/remind_me_head_txt"
android:layout_marginTop="5dp"
android:textSize="20sp"
android:text="Example one"
android:textStyle="bold"/>

</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="120dp"
android:layout_weight="1">

<Switch
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:showText="false"
android:id="@+id/remind_me_switch"/>

</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1">

<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:id="@+id/remind_me_list_img"
android:src="@drawable/list_menu" />

</LinearLayout>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/remind_me_title_txt"
android:text="Sample Head"
android:textSize="12sp"/>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/remind_me_content_txt"
android:text="Content"
android:textSize="12sp"/>

</LinearLayout>

</LinearLayout>

目前我不知道如何实现这一目标。

我们可以改变开关的形状吗?可能吗?

最佳答案

这些形状根据设备而变化,例如在 Redmi Note 中显示为矩形,在 Moto 中显示为圆形。

不过,为了实现这一点,您可以使用如下所示的切换实现:具有三种样式:

  • offToggle(样式关闭)
  • onToggle(开启样式)
  • custom_toggle(样式自定义切换)

风格关闭

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
android:visible="true">
<solid android:color="@android:color/holo_blue_dark" />
</shape>

着装风格

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
android:visible="true">
<solid android:color="@android:color/black" />
</shape>

现在我们将在布局中使用的集体样式 Style Custom Toggle

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/offToggle" android:state_enabled="false"/>
<item android:drawable="@drawable/onToggle" android:state_pressed="true"/>
<item android:drawable="@drawable/onToggle" android:state_checked="true"/>
<item android:drawable="@drawable/onToggle" android:state_focused="true"/>
<item android:drawable="@drawable/offToggle"/>
</selector>

现在只需在您的布局中使用此样式,例如:

<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn=""
android:textOff=""
android:background="@drawable/custom_toggle" />

提示:要从开关/切换中删除文本,请使用 android:textOn="" android:textOff=""

关于android - 如何在android中改变开关的形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40415851/

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