如何在 Xamarin Android 的 LinearLayout 上获得圆角?

问题描述 投票:0回答:1

我想知道如何圆角我的 Xamarin Android 线性布局。我有以下 xml 布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="horizontal"
   android:layout_width="wrap_content"
   android:layout_height="match_parent"
    android:backgroundTint="@android:color/holo_green_dark">

  <ImageView
    android:id="@+id/iv1"
    android:src="@android:drawable/ic_media_play"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

  <TextView
    android:id="@+id/tv1"
    android:text="S"
    android:layout_marginLeft="22dp"
    android:layout_marginTop="4dp"
    android:layout_marginRight="0dp"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:textSize="20dp"
    android:textAlignment="center"
    />
  <TextView
    android:id="@+id/tv2"
    android:text="S"
    android:layout_marginLeft="0dp"
    android:layout_marginTop="4dp"
    android:layout_marginRight="5dp"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:textSize="20dp"
    android:textAlignment="center"
    />
</LinearLayout>

和按以下方式创建的View

public void CreateFloatingWindow()
{
    windowManager = GetSystemService(WindowService).JavaCast<IWindowManager>();
    LayoutInflater mLayoutInflater = LayoutInflater.From(ApplicationContext);
    floatView = mLayoutInflater.Inflate(Resource.Layout.floatview, null);

    // set LayoutParam
    layoutParams = new WindowManagerLayoutParams();
    layoutParams.Width = 400;
    layoutParams.Height = 200;

    layoutParams.X = 210;
    layoutParams.Y = 300;

    if ((int)Build.VERSION.SdkInt >= 26)
    {
        layoutParams.Type = WindowManagerTypes.ApplicationOverlay;
    }
    else
    {
        layoutParams.Type = WindowManagerTypes.Phone;
    }

    layoutParams.Flags = WindowManagerFlags.NotTouchModal;
    layoutParams.Flags = WindowManagerFlags.NotFocusable;

    //set round corners
    GradientDrawable shape = new GradientDrawable();
    shape.SetCornerRadius(CORNER_RADIUS);
    floatView.SetBackground(shape);

    windowManager.AddView(floatView, layoutParams);

}

我最终得到的结果如下。内部形状的角是圆形的,但角外是 black 背景。

如何让黑角透明。有什么想法吗?

更新

再次尝试layout_bg.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#00FFFF"/>
    <stroke android:width="3dp" android:color="#B1BC76" />
    <corners android:radius="40dp"/>
    <padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="horizontal"
   android:layout_width="wrap_content"
   android:layout_height="match_parent"
    android:background="@drawable/layout_bg">
...

新形状的绿色描边和青色背景都透出来了,但边角还是黑色

android xamarin xamarin.android android-linearlayout
1个回答
0
投票

我已经添加了一些代码片段和输出图像,正如你上面在你的疑问/问题/问题中所说的

在主 xml 文件下面

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:padding="10dp"
   
    android:background="@drawable/temp">

    <ImageView
        android:id="@+id/iv1"
        android:src="@android:drawable/ic_media_play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/tv1"
        android:text="S"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="4dp"
        android:layout_marginRight="0dp"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:textSize="20dp"
        android:textAlignment="center"
        />
    <TextView
        android:id="@+id/tv2"
        android:text="S"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="4dp"
        android:layout_marginRight="5dp"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:textSize="20dp"
        android:textAlignment="center"
        />
</LinearLayout>

这里是所需形状的可绘制对象

    <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners
        android:topLeftRadius="20dp"
        android:topRightRadius="20dp"
        android:bottomLeftRadius="20dp"
        android:bottomRightRadius="20dp" />
    <solid
        android:color="@android:color/holo_green_dark"/>

</shape>

您可以根据需要自定义转角半径

您可以根据需要对齐视图...我希望这对您有用 提前谢谢你!

© www.soinside.com 2019 - 2024. All rights reserved.