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

ukqbszuj  于 2023-02-27  发布在  Android
关注(0)|答案(2)|浏览(194)

我想知道如何圆我的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>

以及以下列方式创建的视图

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);

}

我的结果是这样的。内部形状的角是圆的,但是角的外面是一个黑色背景。

我怎样才能使黑色的角落透明。有什么想法吗?

    • 更新**

使用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">
...

新形状的绿色笔触和青色背景将显示出来,但角仍然是黑色的。

    • 更新2**

我试过设置

android:alpha="0.1"

但这只会使图像和文本控件透明,而不是背景本身。背景保持黑色。

2cmtqfgy

2cmtqfgy1#

我已经添加了一些代码片段和输出图像,如您在上面的疑问/问题/问题中所述
在主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>

您可以根据自己的需要定制圆角半径x1c 0d1x
您可以根据您的需要对齐视图...我希望这将为您工作,提前谢谢!!

kpbpu008

kpbpu0082#

问题出在windowManager创建的后台。
为了解决这个问题,我修改了WindowManagerLayoutParams,如下所示,效果很好:

// set LayoutParams
WindowManagerTypes type = ((int)Build.VERSION.SdkInt >= 26 /*Build.VERSION_CODES.O*/) ? WindowManagerTypes.ApplicationOverlay : WindowManagerTypes.Phone;

layoutParams = new WindowManagerLayoutParams(type, WindowManagerFlags.NotFocusable | WindowManagerFlags.NotTouchModal, 
               Format.Transparent);

注意在得出我的解决方案时,我还参考了此问题的答案:Android: Transparend background when using WindowManager

谢谢大家的帮助!

相关问题