当前位置: 首页 > 知识库问答 >
问题:

线性布局上的圆角[闭合]

萧星火
2023-03-14

我有一个线性布局,有2个不同背景颜色的文本视图。我想使整个视图(线性布局)圆角。我试图将它封闭在MaterialCardview中(因为当我在里面设置整个片段布局时,我能够达到这种效果),但由于某种原因,它不起作用。我需要做什么来实现视图的圆角?

<android.support.design.card.MaterialCardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginEnd="24dp"
        android:layout_marginBottom="8dp"
        app:cardCornerRadius="20dp"
        app:cardElevation="2dp"
        app:cardBackgroundColor="@color/Transparent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tutTitle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/tutorial_title"
                android:text="Text 1"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textColor="@color/White"
                android:textSize="16sp" />

            <TextView
                android:id="@+id/tutBody"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/tutorial_body"
                android:padding="10dp"
                android:drawableLeft="@drawable/image"
                android:drawablePadding="10dp"
                android:text="This is a hint"
                android:textColor="@color/main_dark_grey"
                android:textSize="16sp" />
        </LinearLayout>

    </android.support.design.card.MaterialCardView>

注意:我知道有些人可能建议使用背景为圆形的xml drawable。这将不起作用,因为儿童背景色将接管透明度,并将保持锐利的边缘

共有3个答案

冯亮
2023-03-14

我想使整个视图(线性布局)圆角。

由于线性布局覆盖了所有卡视图,因此整个视图就是卡视图
因此,不要将透明背景设置为CardView,而是将其设置为LinearLayout,将背景颜色设置为CardView,您将看到圆角
如果将透明颜色设置为cardwiewLinearLayout由于没有角,您希望如何看到圆角?

洪鸿
2023-03-14

试试这个布局。希望这是给你的预期结果,你想要的。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="16dp">



<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/layout_bg"
    >
    <TextView
        android:id="@+id/que"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:gravity="center_vertical"
        android:paddingLeft="15dp"
        android:text="@string/tvque"
        android:textColor="#cbd3db" />
    </RelativeLayout>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/layoutbg1"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/des"
        android:padding="16dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tvdes"
        android:textColor="#2c365a"
        />
</RelativeLayout>

</LinearLayout>

layout_bg

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
 <solid android:color="#3e4874"/>
 <corners android:topLeftRadius="6dp" android:topRightRadius="6dp"
    android:bottomLeftRadius="0.1dp" android:bottomRightRadius="0.1dp"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" 
         android:bottom="0dp" />
</shape>

layout_bg1

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#fff"/>
<corners android:topLeftRadius="0.1dp" android:topRightRadius="0.1dp"
    android:bottomLeftRadius="6dp" android:bottomRightRadius="6dp"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" 
         android:bottom="0dp" />
</shape>
宗晟
2023-03-14

res/可绘制/background.xml

<shape  xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="#00ffffff"/>
    <corners android:bottomRightRadius="8dp"
       android:bottomLeftRadius="8dp"  
       android:topRightRadius="8dp"
       android:topLeftRadius="8dp"/>
</shape>

活动xml

<LinearLayout
        android:layout_width="match_parent"
        android:background="@drawable/background"
        android:layout_height="wrap_content"
        android:orientation="vertical">
 类似资料:
  • 我如何做一个圆角布局?我想对我的应用圆角。

  • 我试图在另一个活动的顶部显示一个半透明背景的活动,并在屏幕中央显示一个drawable。drawable有圆角,我设置了一个圆角矩形作为布局的背景。这个形状的背景是可绘制的。问题是我仍然在圆角的drawable后面得到一个黑色的方形边框。有没有办法去掉那个黑边? 我想我不能发布图片,因为我没有声誉? 这是布局的xml: 以下是形状的xml:

  • 主要内容:本节引言,1.本节学习图,2.weight(权重)属性详解:,3.为LinearLayout设置分割线,4.LinearLayout的简单例子:,5.注意事项:本节引言 本节开始讲Android中的布局,Android中有六大布局,分别是: LinearLayout(线性布局),RelativeLayout(相对布局),TableLayout(表格布局) FrameLayout(帧布局),AbsoluteLayout(绝对布局),GridLayout(网格布局) 而今天我们要讲解的就是

  • 本文向大家介绍Android LinearLayout 线性布局,包括了Android LinearLayout 线性布局的使用技巧和注意事项,需要的朋友参考一下 示例 LinearLayout是一种ViewGroup将其子级排列在单列或单行中的。可以通过调用方法setOrientation()或使用xml属性来设置方向android:orientation。 垂直方向:android:orien

  • 在上一节中,我们讲到了所有的 Layout 都是从 ViewGroup 继承而来,它可以包含若干 View 并按照指定的规则将这个 View 摆放到屏幕上。那么接下来的章节我们就来学习一下 Android 的 UI 布局,Android 原生有六大布局,分别是: LinearLayout(线性布局)、RelativeLayout(相对布局)、TableLayout(表格布局)、FrameLayou

  • 我有这个布局。 但问题是,ExpandableListView总是以屏幕为中心,尽管父布局中有重力。我想要的是让它从ListView结束的地方开始。知道是什么原因吗?我应该使用滚动视图而不是线性布局吗? 我希望可扩展的listview出现在listview下方。