【问题标题】:Change Size of Xamarin Android Checkbox更改 Xamarin Android 复选框的大小
【发布时间】:2017-08-11 08:42:31
【问题描述】:

我正在尝试更改 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,则复选框会被剪裁。

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

我尝试过调整本机控件的大小以及布局参数等内容。没用。

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

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

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

【问题讨论】:

    标签: xamarin.android android-checkbox


    【解决方案1】:

    尝试使用比例:

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

    【讨论】:

    • 感谢史蒂夫的回答。我应该提到我尝试了 ScaleX/Y 并且它有效。我希望有一种方法可以设置复选框的特定大小。谢谢。
    • 我找不到比史蒂夫提供的更好的答案,因此将他的答案标记为已接受。谢谢史蒂夫。
    • 如果有人感兴趣,这也适用于 Xamarin Forms
    猜你喜欢
    • 2012-02-05
    • 1970-01-01
    • 2012-04-26
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-23
    • 2014-05-09
    相关资源
    最近更新 更多