nbr*_*osz 5 c# android xamarin.android xamarin xamarin.forms
Xamarin Forms上的按钮视图在部署到Android时似乎得到了额外的填充。我已在Android项目下将Resources / values / styles.xml设置为默认情况下,所有间距,填充和边距均为0,但仍然应用了额外的填充:
<item name="android:radius">0.0px</item>
<item name="android:shadowRadius">0.0</item>
<item name="android:spacing">0.0px</item>
<item name="android:padding">0.0px</item>
<item name="android:layout_margin">0.0px</item>
Run Code Online (Sandbox Code Playgroud)
请查看以下比较,以了解我在说什么(左侧的iOS,右侧的Android)。请注意,iOS中的视图如何彼此齐平(应如此),而Android中则存在多余的空白。显示内容由绝对布局和堆栈布局,几个按钮和一个标签(长标签)的组合组成。不必担心iOS示例中的尺寸和排列不完美;这是故意的。iOS的显示正是我希望它们都看起来的样子。
理想情况下,我想要一个Android风格或一个简单的“一刀切”的所有代码段,这些代码段可用于消除Android的固定间距。在两个设备上的视图上均以编程方式设置了边距,间距和填充,因此这不应成为问题的根源。
请注意,我将C#与Xamarin表单结合使用-而不是XAML。因此,请提供适用于两者的答案,或者至少适用于Xamarin Forms的纯编程实现的答案。
这是另一个示例(左侧为iOS,右侧为Android)。最外面的布局是一个网格,包含六个非布局视图(两个按钮,两个标签和两个条目),以及底部的堆栈布局(它又包含一个按钮,一个标签和一个条目)。从颜色的差异可以看出,除了按钮之外,其他所有内容都正确排列。仅使用视图边距设置对齐方式;默认间距和填充全部归零(来自Android样式,请参见上文)。
Try this:
In your Android Project open your Styles.xml
Inside the MyTheme.Base style (I am assuming is called like this based on the default Xamarin.Forms template) add the following line:
<item name="android:buttonStyle">@style/MyButton</item>
Run Code Online (Sandbox Code Playgroud)
Create a new style with name MyButton
<style name="MyButton" parent="android:Widget.Material.Button">
<item name="android:layout_margin">0dip</item>
<item name="android:background">@drawable/button_ripple</item>
</style>
Run Code Online (Sandbox Code Playgroud)
In the Drawable folder add a new XML file called button_ripple.xml and paste the following code:
<?xml version="1.0" encoding="utf-8"?>
<!-- in drawable folder-->
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<solid android:color="?android:colorAccent" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#ff0000" />
</shape>
</item>
</ripple>
Run Code Online (Sandbox Code Playgroud)
This file is necessary because when the background of a button in Android is changed the ripple effect (the effect when tapping on it) is lost.
If you don't care about the effect go back to the style file and the MyButton style can be just something like this:
<style name="MyButton" parent="android:Widget.Material.Button">
<item name="android:layout_margin">0dip</item>
<item name="android:background">#ff0000</item>
</style>
Run Code Online (Sandbox Code Playgroud)
This might help you without CustomRenderers.-
| 归档时间: |
|
| 查看次数: |
2594 次 |
| 最近记录: |