![](/img/trans.png)
[英]Change the Background Color of an ListView Item on Xamarin Forms
[英]Xamarin Android: Change the background color of a GridView item when clicked
我正在搜索如何在单击时更改单击的gridview项的背景颜色,然后返回到正常颜色
我希望当我单击时,Gridview项的背景颜色是橙色,然后过一会儿,背景又变成白色。
这是我找到的,但是“设备”未知。
e.View.SetBackgroundColor(Color.White);
Device.StartTimer(TimeSpan.FromSeconds(0.25), () =>
{
e.View.SetBackgroundColor(Color.Orange);
return false;
});
我尝试了这个:
1)通过在值中创建colors.xml来定义颜色
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="pressed_color">#972234</color>
<color name="default_color">#000000</color>
</resources>
2)在drawable中创建bg_key.xml
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="@color/pressed_color"/>
<item
android:state_pressed="true"
android:drawable="@color/pressed_color"/>
<item
android:drawable="@color/default_color" />
</selector>
3)将android:listSelector和listSelector设置为GridView
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:listSelector="@drawable/bg_key"
android:background="@color/default_color"
/>
它在我的侧菜单上起作用,但在我的gridview上却不起作用...我的网格视图由ImageView和TextView组成,这是问题吗?
另外,我应该更改(针对我的侧菜单)更改字体颜色而不更改背景颜色吗?
您不能使用Device
-Class,因为它仅在Xamarin.Forms中可用,而在本机Xamarin中不可用。
但是您可以使用System.Timers.Timer
类在一段时间后更改颜色:
var t = new System.Timers.Timer();
t.Interval = 250; // In miliseconds
t.Elapsed += (sender, args) =>
{
// Change color back on the UI-Thread
RunOnUiThread(() =>
{
e.View.SetBackgroundColor(Color.Orange);
});
};
t.Start();
要点: Elapsed
-Event 不会在UI-Thread上调用 。 因此,要更改UI上的某些内容(就像您的背景色一样),您需要在UI线程上执行此操作。
Device.StartTimer
中使用Device.StartTimer使用设备时钟功能来启动循环计时器。 它在本机中不可用。
我希望您可以制作自定义样式。
尝试这个 :
1)通过在values
创建colors.xml
来定义颜色
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="pressed_color">#972234</color>
<color name="default_color">#000000</color>
</resources>
2)在drawable
创建bg_key.xml
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="@color/pressed_color"/>
<item
android:state_pressed="true"
android:drawable="@color/pressed_color"/>
<item
android:drawable="@color/default_color" />
</selector>
3)将android:listSelector
和listSelector设置为GridView
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:listSelector="@drawable/bg_key"
android:background="@color/default_color"
/>
这是我的代码:
Main.axml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#EBEAEF">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:paddingLeft="5dp"
android:background="#282059"
android:title="test"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" />
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<!-- The Main Content View -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/logoBackgroud"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="@drawable/icon_background" />
<GridView
android:id="@+id/grid_view_image_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="350dp"
android:layout_margin="10dp"
android:gravity="center"
android:numColumns="auto_fit" />
</RelativeLayout>
gridview.axml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EBEAEF">
<RelativeLayout
android:layout_height="90dp"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_margin="25dp"
android:listSelector="@drawable/bg_key"
android:background="#F4F4F6">
<!-- Letter yellow color = #FAB322 -->
<TextView
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="95dp"
android:layout_height="fill_parent"
android:textSize="66sp"
android:textColor="#0071CF"
android:background="@color/white"
android:layout_centerVertical="true"
android:layout_gravity="left"
android:gravity="center_vertical|center_horizontal"
android:text="A"
android:paddingBottom="5dp"
android:id="@+id/textViewLetter" />
<TextView
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:paddingLeft="15dp"
android:layout_toRightOf="@+id/textViewLetter"
android:layout_centerVertical="true"
android:layout_marginRight="35dp"
android:textSize="22sp"
android:gravity="center_vertical"
android:background="#F4F4F6"
android:textColor="#262057"
android:textStyle="bold"
android:listSelector="@drawable/bg_key"
android:id="@+id/textViewFileName" />
<ImageView
android:layout_width="35dp"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:layout_alignParentRight="true"
android:id="@+id/imageViewIcon" />
</RelativeLayout>
</LinearLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.