android - "dp"具有多个矩形的 Xamarin Android Layer-List 中的问题

标签 android visual-studio xamarin

我在 Visual Studio 中使用 Xamarin Android 并尝试执行一项显示两个矩形的简单任务,一个在另一个之上。当我使用“px”的测量大小时,一切都会按预期工作,但是当我使用“dp”时,第二个矩形不会被渲染。这是我的带有矩形的 xml 文件:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="0dp" >
    <shape android:shape="rectangle">
        <solid android:color="#6EF562" />
    </shape>
</item>
<item android:top="57dp">
    <shape android:shape="rectangle">
        <solid android:color="#000000" />
    </shape>
</item>

请注意上面的“dp”。该代码不起作用。但如果改为“px”,则按预期工作。

这是我的 Main.axml 文件的来源:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#E6EDF0"
android:paddingTop="50dp">
<Button
    android:id="@+id/MyButton"
    android:layout_width="178.1dp"
    android:layout_height="wrap_content"
    android:text="@string/Hello"
    android:layout_gravity="center"
    android:padding="5dp"
    android:layout_margin="5dp"
    android:textColor="#000000"
    android:background="@drawable/splitColors" />

显然,出于缩放原因,我想使用“dp”而不是像素“px”,以适应不同的设备外形尺寸,但我无法让它工作。

最佳答案

按钮的高度,设置为 wrap_content 。在您的绘图中,您指定要以 57dp 绘制黑色底部。因此,如果您的按钮小于 57dp,它将永远不会显示。这就是为什么它以像素为单位完成工作,因为 57px 并没有那么高。因此,如果您想使用 DP,请确保您的按钮至少与您给出的值 <item android:top="57dp"> 一样高。

关于android - "dp"具有多个矩形的 Xamarin Android Layer-List 中的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32741640/

相关文章:

c# - 使用 C# 更改 ASP.net 中的背景图像

c# - 调试时,我可以在 LINQ 中查看导致异常的对象吗?

c# - Xamarin.iOS 上的 SignalR - 随机无法调用 Hub 方法

android - Visual Studio 2015 中的 "No devices available"

android - Google Play 控制台不会减少应用程序下载大小

android - 如何使用约束布局拆分两种背景颜色?

Android - 以编程方式设置两个重力参数

android - Android 的 WebView 中的 shouldOverrideUrlLoading 未运行

android - 无法在使用 Kotlin 的方法调用中为接口(interface)传递 lambda(就像 setOnClickListener 的做法一样)

c# - Visual Studio : Programmatically Create Project Items in project directory