警告:这个问题中的XML是错误的,请在混淆自己之前阅读答案!
我一直在墙上撞了一会儿.以下帖子阐明了这一主题,但未能解决我的问题: Android ListView状态列表未通过自定义选择器显示默认项目背景和ListView项目背景
当我选择列表项时,正确的九补丁背景显示完美,但我无法获得最初显示的默认九补丁背景.在我看来,我需要以某种方式设置默认项目背景,但我无法弄清楚如何这样做.
列表显示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/shopListHeader"
/>
<ListView
android:id="@+id/shopList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:dividerHeight="0px"
android:divider="#FFFFFFFF"
android:listSelector="@drawable/shop_list_selector"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
选择:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:visible="true">
<!-- the list items are enabled and being pressed -->
<item
android:state_pressed="true"
android:drawable="@drawable/shop_list_item_pressed" />
<item
android:state_selected="true"
android:textColor="#FFFFFFFF" />
</selector>
Run Code Online (Sandbox Code Playgroud)
背景:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:visible="true">
<item
android:state_selected="true"
android:drawable="@android:color/transparent" />
<item
android:drawable="@drawable/shop_list_item" />
</selector>
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我已经从参考文献中删除了示例.
您可能还注意到后台选择器未在任何位置引用.我开始在随机的地方扔它(如果应用程序编译添加要么没有效果或导致强制关闭)
当选择一个项目但没有按下时,我也试图阻止文本的颜色变为黑色和灰色(可以通过滚动列表来完成).由于我的背景在中心是黑色,因此选中时文本会变得部分不可见.据我所知,这个添加(选择器中的最后一个项目节点)什么都不做.
有没有人有任何想法让这个荒谬耗时的功能工作?
我正在使用带有customCell的UITableView(CustomCell有2个标签和一个ImageView).
在正常模式下,我需要所有细胞的白色.
如果用户按下特定单元格,则该单元格的颜色应为灰色(其余单元格应为白色)
如果用户释放相同的单元格,该单元格的颜色应为橙色(其余单元格应为白色)
我怎么能搞清楚?
我已经尝试使用setSelectedBackground,willSelectRowAtIndexPath和Gestures方法.但无法看到同一Cell的这3种颜色状态.这两个州中的任何一个都在一起工作.
任何想法我怎样才能实现相同的功能?
我使用选择器在android中实现了相同的功能.我想在iPhone中使用相同的功能.任何帮助?
提前致谢!
开始了.我有一个非按下状态背景为透明的按钮.按下按钮后,背景不再透明(参见图片).我为你的所有图像使用九个补丁.
这是我的选择器:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/starfighterstartbtndown" android:state_pressed="true"/>
<item android:drawable="@drawable/starfighterstartbtn"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
这是布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/mainMenuImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/starfighter" >
</ImageView>
<RelativeLayout
android:id="@+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="@android:color/transparent"
android:clickable="true"
android:hapticFeedbackEnabled="true"
android:src="@drawable/startselector" >
</ImageButton>
<ImageButton
android:id="@+id/btnExit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:clickable="true"
android:src="@drawable/exitselector"
android:background="@android:color/transparent"
android:hapticFeedbackEnabled="true" >
</ImageButton>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
未点击:

点击:

我也试过以下方法没有运气
button.getBackground().setAlpha(0);
button.setBackgroundColor(Color.TRANSPARENT);
Run Code Online (Sandbox Code Playgroud)
还在onClickListener()内部尝试了以下内容
view.getBackground().setAlpha(0);
view.setBackgroundColor(Color.TRANSPARENT);
button.getBackground().setAlpha(0);
button.setBackgroundColor(Color.TRANSPARENT);
Run Code Online (Sandbox Code Playgroud)
仍然没有运气.祝好运 :)
我正在开发一个应用程序,我在其中使用复选框并在其上应用选择器。我的复选框代码如下:
<CheckBox
android:id="@+id/checkBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.5"
android:button="@drawable/custom_checkbox"
android:clickable="false"
android:focusable="false"
android:gravity="center" />
Run Code Online (Sandbox Code Playgroud)
custom_checkbox.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/checkbox_checked" android:state_checked="true"/>
<item android:drawable="@drawable/checkbox_checked" android:state_pressed="true"/>
<item android:drawable="@drawable/checkbox_unchecked"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
checkbox_checked
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/green_tic"/>
<item>
<shape android:shape="rectangle" >
<corners android:radius="@dimen/corner_radius_for_ask_option"/>
<size
android:height="20dp"
android:width="20dp" />
</shape>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
checkbox_unchecked
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<stroke
android:width="2px"
android:color="@color/color_light_grey" />
<corners android:radius="@dimen/corner_radius_for_ask_option" />
<size
android:height="20dp"
android:width="20dp" />
</shape>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
上面的代码在 Note 3 和同类手机上运行良好。看起来像 …
checkbox android android-layout android-checkbox android-selector
我有一个图像视图,它使用选择器
<ImageView
android:id="@+id/image_view"
android:src="@drawable/ic_back_hand_24_selector"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)
选择器根据状态在 2 个矢量可绘制图像之间进行选择
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_baseline_back_hand_24" android:state_selected="true" />
<item android:drawable="@drawable/ic_outline_back_hand_24" />
</selector>
Run Code Online (Sandbox Code Playgroud)
这非常方便,我们可以通过以编程方式将状态设置为 True 或 False 来交换图像
theImageView.setOnClickListener {
this.isSelected = !this.isSelected
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我将其放在 Jetpack Compose 上,Icon如下所示
Icon(painterResource(id = R.drawable.ic_baseline_back_hand_24), contentDescription = "")
Run Code Online (Sandbox Code Playgroud)
它崩溃说明
java.lang.IllegalArgumentException: Only VectorDrawables and rasterized asset types are supported ex. PNG, JPG
Run Code Online (Sandbox Code Playgroud)
Jetpack Compose Icon 没有办法加载选择器 XML 吗?
我有一个ExpandableListView.当孩子被扩大时,应该被视为"被选中".选择孩子时,孩子和孩子的孩子都应该显示不同的背景(向用户显示当前所选项目).
现在这就是我所拥有的:
XML
<ExpandableListView
android:id="@+id/listViewAccepted"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:choiceMode="singleChoice"
android:listSelector="@drawable/custom_selector" >
</ExpandableListView>
Run Code Online (Sandbox Code Playgroud)
ListView
setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View view,int groupPosition, long id) {
view.setSelected(true);
parent.setItemChecked(groupPosition, true);
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
选择器
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<solid android:color="@color/holo_blue_light" />
</shape>
</item>
<item android:state_selected="true">
<shape>
<solid android:color="@color/green" />
</shape>
</item>
<item android:state_checked="true">
<shape>
<solid android:color="@color/green" />
</shape>
</item>
<item android:state_focused="true">
<shape>
<solid android:color="@color/green" />
</shape>
</item>
<item android:state_activated="true">
<shape>
<solid android:color="@color/green" />
</shape>
</item>
<item>
<shape>
<solid android:color="@color/transparent" …Run Code Online (Sandbox Code Playgroud) 我是android的新手,我创建了简单的应用程序.当点击按钮textview应该改变该功能工作正常但按钮图像按下时不会改变.
这是我在fragment_main.xml中的按钮
<Button
android:id="@+id/btnCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/plus"
android:onClick="changeText" />
Run Code Online (Sandbox Code Playgroud)
这里是drawable文件夹中custom.xml文件的代码.(没有默认的drawable文件夹,我创建了它)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/plus_selected" android:state_pressed="true"/>
<item android:drawable="@drawable/plus_highlighted" android:state_focused="true"/>
<item android:drawable="@drawable/plus"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
plus_selected.png,plus_highlighted.png和plus.png文件位于drawable-hdpi文件夹中
这是我的java代码
public void changeText(View v) {
TextView tvText = (TextView) findViewById(R.id.tvDisplay);
tvText.setText("you cliked the button");
}
Run Code Online (Sandbox Code Playgroud)
当单击按钮textview更改为"您单击按钮"但plus.png图像始终作为按钮图像存在.按下时它不会改变
我该如何解决这个问题?
我有一个按钮
<Button
android:id="@+id/loginButton"
android:layout_width="35dp"
android:layout_height="35dp"
android:background="@drawable/button_shape"
android:text="@string/login"
android:textColor="#ffffff"
android:textSize="12sp" />
Run Code Online (Sandbox Code Playgroud)
以下是button_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/button_gradient_login" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/button_gradient_login" />
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size android:height="16dp" />
<solid android:color="@color/valid_btn_clr"></solid>
<corners android:radius="10dp"></corners>
</shape>
</item>
Run Code Online (Sandbox Code Playgroud)
以下是button_gradient_login.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:angle="360" android:startColor="@color/red" android:endColor="@color/green"/>
<corners android:radius="4dp"></corners>
</shape>
</item></layer-list>
Run Code Online (Sandbox Code Playgroud)
现在我想更改button_shape.xml的背景颜色,并在运行时按下button_gradient_login.xml的开始和结束颜色取决于我的条件
我有一个回收器视图,我需要使用颜色选择器之一(具体取决于数据绑定中的值)来更改 TextView 颜色。
我有两个选择器:
颜色/selector_item_text.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/white" android:state_checked="true" />
<item android:color="@color/white" android:state_pressed="true" />
<item android:color="@color/white" android:state_activated="true" />
<item android:color="@color/black" />
</selector>
Run Code Online (Sandbox Code Playgroud)
颜色/selector_item_textwithspecial.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/pink" android:state_checked="true" />
<item android:color="@color/pink" android:state_pressed="true" />
<item android:color="@color/pink" android:state_activated="true" />
<item android:color="@color/orange" />
</selector>
Run Code Online (Sandbox Code Playgroud)
我将它绑定到我的 TextView 上,如下所示:
<TextView android:text="@{data.displayPrice}"
android:textColor="@{data.isSpecial ? @color/selector_item_textwithspecial : @color/selector_item_text}"
style="@style/ProductPrice"/>
Run Code Online (Sandbox Code Playgroud)
问题是 TextView 颜色始终是橙色(如果有特殊)或黑色。选择永远不会改变颜色。但是,如果我删除数据绑定,它就会按预期工作。
例如,下面的代码将使 TextView 变成粉色(选中时)和橙色(未选中时)
<TextView android:text="@{data.displayPrice}"
android:textColor="@color/selector_item_textwithspecial"
style="@style/ProductPrice"/>
Run Code Online (Sandbox Code Playgroud)
知道如何解决这个问题吗?
谢谢...
android textcolor android-selector android-databinding android-text-color
我EditText有一些颜色,但是当我输入一些文本时,它应该会改变。说它是灰色,我开始输入,它是黄色。这EditText也是重点,因此当此活动开始时-我已经可以输入文本了。我如何尝试:
<style name="LoginEnterField">
<item name="android:textColorHint">@color/black</item>
<item name="android:textColor">@color/black</item>
<item name="android:background">@drawable/selector_edittext</item>
<item name="android:textSize">@dimen/general_text_size</item>
<item name="android:paddingLeft">@dimen/activity_inner_horizontal_margin</item>
<item name="android:paddingRight">@dimen/activity_inner_horizontal_margin</item>
</style>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/grey"/>
<corners android:radius="5dp" />
</shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/yellow"/>
<corners android:radius="5dp" />
</shape>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@drawable/roundbox_active_style"/>
<item android:drawable="@drawable/roundbox_inactive_style" />
</selector>
Run Code Online (Sandbox Code Playgroud)
我认为关键在于此选择器。我的EditText更改颜色,但始终为黄色。仅当输入一些文字时,如何使其变成黄色?我无法进行任何代码更改!这很重要。我不能仅添加或更改xml文件。
编辑:这是不可能只从xml,所以进行代码更改。
android-selector ×10
android ×9
nine-patch ×2
android-xml ×1
checkbox ×1
imageview ×1
iphone ×1
java ×1
textcolor ×1
uitableview ×1