android - 如何增加开关控件 Xamarin 表单的大小?

标签 android xamarin multiplatform

我最近刚开始使用 Xamarin Forms,并负责将工作中的 iOS 项目切换到 Droid。这是一个多平台项目,所以我在共享项目中创建开关,但试图在 .Droid styles.xml 或自定义渲染器中管理它的样式。我需要为平板电脑提供更大的控件。

使用 styles.xml,我能够使用这一行更改开关控件的宽度:

<item name="switchMinWidth">120dp</item>

但据我所知,无法以这种方式更改控件的高度。我试过使用自定义渲染器并使用了控件 (Android.Switch.Widget) 的 SetHeight、SetMinHeight 和 SetMinimumHeight 方法,但没有任何效果。

这是开关目前的样子:

https://imgur.com/XIPp6vV

我还尝试在 xaml 本身中执行 HeightRequest,但它不会更改控件的实际高度。我可以做些什么来让这些开关更高一点吗?

最佳答案

How to increase size of switch control Xamarin forms?

Switch高度由 <switch android:track="@drawable/shape_mythumb".../> 控制,轨道的高度决定了 Switch的整体高度。所以你可以在你的自定义中添加这个属性 SwitchRenderer :

public class MySwitchRenderer : SwitchRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Switch> e)
    {
        base.OnElementChanged(e);
        if(Control != null)
        {
            Control.SetTrackResource(Resource.Drawable.track);
        }
    }
}

您需要定制一个 Track布局,你可以绘制任何你想要的形状。

示例:

轨道.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item
      android:drawable="@drawable/switch_track_on"
      android:state_checked="true"/>

  <item
      android:drawable="@drawable/switch_track_off"
      android:state_checked="false"/>

</selector>

switch_track_on.xml

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

  <corners
       android:radius="15dp" />
  <size
       android:width="75dp"
       android:height="25dp" />
  <solid
      android:color="#6decacec" />

</shape>

switch_track_off.xml

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

  <corners
      android:radius="15dp" />
  <size
      android:width="75dp"
      android:height="25dp" />
  <solid
      android:color="#6db3b1b3" />
</shape>

关于android - 如何增加开关控件 Xamarin 表单的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47056004/

相关文章:

java - Android ImageView 在图像周围添加空白(尝试修复此处但不成功)

android - android架构组件中executor的使用

c# - 当按钮逻辑失败时在 Storyboard中停止 segue

java - 如何让 Tycho 将特定于平台的片段加载到任何操作系统的测试运行时中?

.net - 如何创建支持多平台的 nuget 包

Android - OnePlus 无法在 ADB 上运行

android.support.v7.widget.PopUpMenu 似乎丢失了

android - 使用内容方案时保存文件的正确方法是什么?

xamarin.ios - Monotouch - 当网络状态改变时得到通知

c++ - 推荐的配置/偏好处理方法