xamarin.android - 更改 Xamarin Android 复选框的大小

标签 xamarin.android android-checkbox

我正在尝试更改 Xamarin Forms 应用程序中 Android 复选框的大小。首先是一些背景。我在 Xamarin Forms 中为复选框设置了一个自定义控件,如下所示:

public class CheckBox : View
{
    public static readonly BindableProperty IsCheckedProperty =
        BindableProperty.Create(nameof(IsChecked), typeof(bool), typeof(CheckBox), false, propertyChanged: IsCheckedChanged);

    // Other properties...
}

在 Android 项目中,我有如下所示的渲染器:

[assembly: ExportRenderer(typeof(CheckBox), typeof(CheckBoxRenderer))]
namespace SmallVictories.Droid.Controls
{

    internal class CheckBoxRenderer : ViewRenderer<CheckBox, Android.Widget.CheckBox>
    {
        protected override void OnElementChanged(ElementChangedEventArgs<CheckBox> e)
        {
            if (Control == null)
            {
                _nativeControl = new Android.Widget.CheckBox(Context);
                SetNativeControl(_nativeControl);
            }

            // Other setup...
        }
    }
}

然后我这样使用页面上的复选框:

<StackLayout Orientation="Horizontal" BackgroundColor="White" Padding="5" Margin="0" HorizontalOptions="FillAndExpand">
    <controls:CheckBox HorizontalOptions="Start" WidthRequest="25" HeightRequest="25" />

如果我将宽度和高度设置为小于 25,则复选框会被剪掉。

Clipped CheckBox

如果我将宽度和高度设置为大于 25,复选框将保持相同大小,但可点击区域会变大。下面是设置为 35 的复选框大小,但它与设置为 25 时的大小相同。

enter image description here

我已经尝试调整诸如 native 控件的大小以及布局参数之类的内容。没用。

// Neither of these work.
_nativeControl.SetWidth(35);
_nativeControl.SetHeight(35);

var lp = _nativeControls.LayoutParameters;
lp.Width = 35;
lp.Height = 35;
_nativeControls.LayoutParameters = lp;

关于我可以尝试的任何其他建议?谢谢。

最佳答案

尝试使用比例:

var checkbox = new CheckBox(Context);
checkbox.ScaleX = 1.5f;
checkbox.ScaleY = 1.5f;

关于xamarin.android - 更改 Xamarin Android 复选框的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45620212/

相关文章:

java - 扫描 ListView 中的复选框的方法

android - 从 Recyclerview 保存复选框状态

java - 检测哪个复选框被触摸?数组 Java Android Studio

.net - 是否可以在 Xamarin.Android 或 Xamarin.iOS 项目中使用 "PackageReference"?

xamarin - Android Google Analytics 无法实例化接收器 com.google.analytics.tracking.android.CampaignTrackingReceiver

android - 带换行符的多行 TextView

android - 在 Android 中使用颜色选择器设置复选框颜色

c# - 系统.MissingMethodException : Method 'MvvmCross.Droid.Views.MvxAndroidViewPresenter..ctor' not found

c# - 我是否必须重新编译 .dll 才能在 Mono Android 项目上使用它?

android - 菜单android上的复选框项目状态