Android 未启用时,ImageView具有不同的图标
在我的应用程序中,我有一个滑块,可以在两个不同的线性布局之间滑动,其中有语言图像按钮,如下所示。我希望这些图标是灰色的,只有当用户点击时才着色。我所有的图标都是灰色的,我只是缺少了xml行。谢谢你抽出时间 编辑 我忘记发布线性布局的xml:Android 未启用时,ImageView具有不同的图标,android,xml,android-ui,Android,Xml,Android Ui,在我的应用程序中,我有一个滑块,可以在两个不同的线性布局之间滑动,其中有语言图像按钮,如下所示。我希望这些图标是灰色的,只有当用户点击时才着色。我所有的图标都是灰色的,我只是缺少了xml行。谢谢你抽出时间 编辑 我忘记发布线性布局的xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center_horizontal" >
<ImageView
android:id="@+id/galika"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:src="@drawable/gallika"
android:layout_weight="1" />
<ImageView
android:id="@+id/germanika"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:src="@drawable/germanika"
android:layout_weight="1" />
<ImageView
android:id="@+id/ellinika"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:src="@drawable/ellinika"
android:layout_weight="1" />
<ImageView
android:id="@+id/agglika"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:src="@drawable/agglika"
android:layout_weight="1" />
</LinearLayout>
使用
选择器
将状态具体化:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="@android:color/white" />
<item android:state_focused="true" android:color="@android:color/white" />
<item android:state_pressed="true" android:color="@android:color/white" />
<item android:color="#f8f8f8" />
</selector>
您还可以在不同的状态下定义其他可绘制图形或其他形状。使用
选择器来具体化状态:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="@android:color/white" />
<item android:state_focused="true" android:color="@android:color/white" />
<item android:state_pressed="true" android:color="@android:color/white" />
<item android:color="#f8f8f8" />
</selector>
您还可以在不同的状态下定义其他可绘制图形或其他形状。使用选择器来具体化状态:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="@android:color/white" />
<item android:state_focused="true" android:color="@android:color/white" />
<item android:state_pressed="true" android:color="@android:color/white" />
<item android:color="#f8f8f8" />
</selector>
您还可以在不同的状态下定义其他可绘制图形或其他形状。使用选择器来具体化状态:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="@android:color/white" />
<item android:state_focused="true" android:color="@android:color/white" />
<item android:state_pressed="true" android:color="@android:color/white" />
<item android:color="#f8f8f8" />
</selector>
您还可以在不同的状态下定义其他可绘制图形或其他形状。您可以将选择器用作ImageViews src
选择器_flag.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/pressed_flag" android:state_pressed="true"/>
<item android:drawable="@drawable/normal_flag"/>
</selector>
您可以将选择器用作ImageViews src
选择器_flag.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/pressed_flag" android:state_pressed="true"/>
<item android:drawable="@drawable/normal_flag"/>
</selector>
您可以将选择器用作ImageViews src
选择器_flag.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/pressed_flag" android:state_pressed="true"/>
<item android:drawable="@drawable/normal_flag"/>
</selector>
您可以将选择器用作ImageViews src
选择器_flag.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/pressed_flag" android:state_pressed="true"/>
<item android:drawable="@drawable/normal_flag"/>
</selector>
创建一个xml名称,比如flag_icon.xml,并将其放入可绘制的文件夹中
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/color_version_image" android:state_pressed="true"/>
<item android:drawable="@drawable/greyscale_version_image"/>
</selector>
如果要以编程方式设置,请尝试以下操作
ImageButton iv = findViewById(R.drawable.your_imagebutton_id);
iv.setImageDrawable(getResources().getDrawable(R.drawable.flag_icon))
创建一个xml名称,比如flag_icon.xml,并将其放入可绘制的文件夹中
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/color_version_image" android:state_pressed="true"/>
<item android:drawable="@drawable/greyscale_version_image"/>
</selector>
如果要以编程方式设置,请尝试以下操作
ImageButton iv = findViewById(R.drawable.your_imagebutton_id);
iv.setImageDrawable(getResources().getDrawable(R.drawable.flag_icon))
创建一个xml名称,比如flag_icon.xml,并将其放入可绘制的文件夹中
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/color_version_image" android:state_pressed="true"/>
<item android:drawable="@drawable/greyscale_version_image"/>
</selector>
如果要以编程方式设置,请尝试以下操作
ImageButton iv = findViewById(R.drawable.your_imagebutton_id);
iv.setImageDrawable(getResources().getDrawable(R.drawable.flag_icon))
创建一个xml名称,比如flag_icon.xml,并将其放入可绘制的文件夹中
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/color_version_image" android:state_pressed="true"/>
<item android:drawable="@drawable/greyscale_version_image"/>
</selector>
如果要以编程方式设置,请尝试以下操作
ImageButton iv = findViewById(R.drawable.your_imagebutton_id);
iv.setImageDrawable(getResources().getDrawable(R.drawable.flag_icon))
选择器应该替换ImageView?我用它替换背景。所以,是的,即使我知道应该避免使用PNG图像,我在这里使用PNG图像,因为它带来的不便小于好处。所以我有透明的图像,我只是在需要时将背景更改为灰度或颜色。选择器应该替换ImageView?我使用它替换背景。所以,是的,即使我知道应该避免使用PNG图像,我在这里使用PNG图像,因为它带来的不便小于好处。所以我有透明的图像,我只是在需要时将背景更改为灰度或颜色。选择器应该替换ImageView?我使用它替换背景。所以,是的,即使我知道应该避免使用PNG图像,我在这里使用PNG图像,因为它带来的不便小于好处。所以我有透明的图像,我只是在需要时将背景更改为灰度或颜色。选择器应该替换ImageView?我使用它替换背景。所以,是的,即使我知道应该避免使用PNG图像,我在这里使用PNG图像,因为它带来的不便小于好处。所以我有透明的图像,当需要的时候,我只是把背景改成灰度或者颜色。有没有办法让它更持久?当图标被释放时,是否不立即更改图标?比如持续时间?有没有办法让它持续更长的时间?当图标被释放时,是否不立即更改图标?比如持续时间?有没有办法让它持续更长的时间?当图标被释放时,是否不立即更改图标?比如持续时间?有没有办法让它持续更长的时间?当图标被释放时,是否不立即更改图标?比如持续时间?