【问题标题】:Going crazy trying to change android theme on Xamarin.Forms疯狂地尝试在 Xamarin.Forms 上更改 android 主题
【发布时间】:2018-12-11 17:04:04
【问题描述】:

请有人帮助我。

我已经尝试了四天来将突出显示的列表视图项的默认橙色更改为其他任何颜色。

我也尝试将我的项目主题设置为 Holo.Light,但无论我做什么都无法做到。

我试过像这样在 MainActivity 中设置它

 [Activity(Label = "Proj", Icon = "@drawable/icon", Theme = "@style/Theme.Holo.Light.DarkActionBar", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]

我尝试在 AndroidManifest 文件中这样设置:

<application android:label="com.proj.app" android:theme="@style/Theme.Holo.Light.DarkActionBar"></application>

我尝试将构建弄乱,并将它们设置为尽可能高的版本(9 和 8.1),并尝试将 minsdk 和 targetsdks 设置为最新的。

没有任何工作。请有人帮助我。

我也尝试过实现自定义渲染器,但也无法做到。

我要做的就是让下面列表视图的突出显示的行不是橙色的。

 <ListView ItemsSource="{Binding Items}" x:Name="StudentsView" HasUnevenRows="True" SeparatorVisibility="Default">
                    <ListView.Header>
                        <StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" >
                            <Image Source="studentsicon.png" Margin="6,10,6,10" ></Image>
                            <Frame BackgroundColor="#222222">
                                <Label Text="Select a student" FontSize="16" TextColor="White"></Label>
                            </Frame>
                            <ActivityIndicator x:Name="actStudents" IsVisible="False" IsRunning="False" Color="#DF304F" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" />
                        </StackLayout>

                    </ListView.Header>
                    <ListView.ItemTemplate>
                        <DataTemplate>
                                <ViewCell>
                                <StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand">
                                    <Frame BackgroundColor="#222222" CornerRadius="0" Margin="10,10,10,0">
                                        <Frame.GestureRecognizers>
                                        </Frame.GestureRecognizers>
                                        <Grid>
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="*"></RowDefinition>
                                            </Grid.RowDefinitions>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="90"></ColumnDefinition>
                                                <ColumnDefinition Width="*"></ColumnDefinition>
                                            </Grid.ColumnDefinitions>
                                            <StackLayout Grid.Column="0" Grid.Row="0" Orientation="Vertical" VerticalOptions="Fill" HorizontalOptions="FillAndExpand">
                                                <Image Source="{Binding Photo}" Aspect="AspectFit" VerticalOptions="FillAndExpand"></Image>
                                            </StackLayout>
                                            <StackLayout Grid.Column="1" Grid.Row="0" Orientation="Vertical" VerticalOptions="Fill" MinimumHeightRequest="400" HorizontalOptions="FillAndExpand">
                                                <Label Text="{Binding FName}" FontSize="20" HorizontalTextAlignment="Center" TextColor="White"></Label>

                                            </StackLayout>
                                        </Grid>
                                    </Frame>
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

【问题讨论】:

标签: android xamarin xamarin.forms


【解决方案1】:

为视图单元创建 Android 渲染器并覆盖 GetCellCore 方法。示例代码如下:

public class CustomViewCellRenderer:ViewCellRenderer
{
    public CustomViewCellRenderer()
    {
    }

    protected override Android.Views.View GetCellCore(Cell item, Android.Views.View convertView, ViewGroup parent, Android.Content.Context context)
    {
         var cell= base.GetCellCore(item, convertView, parent, context);
        if (parent is Android.Widget.ListView listView)
        {
            listView.SetSelector(Android.Resource.Color.Transparent);
            listView.CacheColorHint = Android.Graphics.Color.Transparent;

        }
        return cell;
    }

    protected override void OnCellPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnCellPropertyChanged(sender, e);
    }
}

【讨论】:

  • 所以,这有效,但它只在选择项目时有效,选择后它会回到 orage
  • 让它工作添加到cs:private void ViewCell_Tapped(object sender, System.EventArgs e) { if (lastCell != null) lastCell.View.BackgroundColor = Color.Transparent; var viewCell = (ViewCell)sender; if (viewCell.View != null) { viewCell.View.BackgroundColor = Color.Red;最后一个细胞 = 视图细胞; } }
  • 太好了...欢迎 :-)
【解决方案2】:

我在我的应用中完全不需要自定义渲染器即可完成此操作,方法如下:

我的 style.xml 文件(将其放入 Resources/values 文件夹并使用 AndroidResource 构建操作):

<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <color name="FancyCyanColorOfMine">#00ffff</color>
  <style name="Theme.Main" parent="@android:style/Theme.Holo.Light">
    <item name="android:colorActivatedHighlight">@color/FancyCyanColorOfMine</item>
  </style>
</resources>

然后我在我的 MainActivity 声明中使用这样的主题:

[Activity(Theme = "@style/Theme.Main", [OTHER STUFF OF YOURS])]
public sealed class MainActivity : Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
...
}

还有很多其他的标签用于诸如“colorActivatedHighlight”之类的彩色事物,您可以在此处的 Android 文档中找到这些标签:https://developer.android.com/reference/android/R.attr,也许只需搜索“color”这个词

【讨论】:

    【解决方案3】:

    创建一个扩展的视单元,如下所示:

    public class ExtendedViewCell : ViewCell
    {
        public static readonly BindableProperty SelectedBackgroundColorProperty =
            BindableProperty.Create("SelectedBackgroundColor", 
                                typeof(Color), 
                                typeof(ExtendedViewCell), 
                                Color.Default);
    
        public Color SelectedBackgroundColor
        {
          get { return (Color)GetValue(SelectedBackgroundColorProperty); }
          set { SetValue(SelectedBackgroundColorProperty, value); }
        }
    }
    

    Android 自定义渲染器 当单元格被渲染时,这会捕获 Android 视图单元格的当前(未选择,默认主题)颜色,然后在 Android 视图单元格的 IsSelected 属性从 false 变为 true 时设置视图单元格的背景颜色.然后在翻转回来时将其重置为最初捕获的颜色。

    显然,如果您需要在列表中选择多个项目,这可能会变得更复杂,但这是我的解决方案所需要的。

    assembly: ExportRenderer(typeof(ExtendedViewCell), typeof(ExtendedViewCellRenderer))]
    namespace 
    xamformsdemo.Droid.CustomRenderers
     {
       public class ExtendedViewCellRenderer : ViewCellRenderer
      {
    
    private Android.Views.View _cellCore;
    private Drawable _unselectedBackground;
    private bool _selected;
    
    protected override Android.Views.View GetCellCore(Cell item, 
                                                      Android.Views.View convertView, 
                                                      ViewGroup parent, 
                                                      Context context)
     {
       _cellCore = base.GetCellCore(item, convertView, parent, context);
    
      _selected = false;
      _unselectedBackground = _cellCore.Background;
    
      return _cellCore;
    }
    
    protected override void OnCellPropertyChanged(object sender, PropertyChangedEventArgs args)
    {
      base.OnCellPropertyChanged(sender, args);
    
      if (args.PropertyName == "IsSelected")
      {
        _selected = !_selected;
    
        if (_selected)
        {
          var extendedViewCell = sender as ExtendedViewCell;
          _cellCore.SetBackgroundColor(extendedViewCell.SelectedBackgroundColor.ToAndroid());
        }
        else
        {
          _cellCore.SetBackground(_unselectedBackground);
        }
       }
      }
     }
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-29
      • 1970-01-01
      • 2013-02-05
      • 1970-01-01
      • 2010-12-12
      • 1970-01-01
      相关资源
      最近更新 更多