线性布局上的圆角

时间:2018-11-29 06:39:26

标签: android android-layout

我有一个线性布局,带有2个具有不同背景颜色的文本视图。我想用圆角制作整个视图(linearlayout)。我尝试将其包含在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。这将无效,因为儿童背景色将取代透明度并保持清晰的边缘

4 个答案:

答案 0 :(得分:1)

尝试这种布局。希望这能给您所需的预期结果。

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

答案 1 :(得分:0)

  

我想用圆角制作整个视图(线性布局)。

由于LinearLayout覆盖了所有CardView,因此整个视图是CardView
所以不要将透明背景设置为CardView,而是将其设置为LinearLayout,将背景颜色设置为CardView,您会看到圆角。
如果您为CardViewLinearLayout都设置了透明颜色,由于没有角点,您如何期望看到圆角?

答案 2 :(得分:0)

res / drawable / background.xml

<shape ... android:shape="rectangle">
    <solid android:color="#00ffffff"/>
    <corners android:bottomRightRadius="8dp"
       android:bottomLeftRadius="8dp"  
       android:topRightRadius="8dp"
       android:topLeftRadius="8dp"/>
</shape>

activity.xml

<LinearLayout
        android:layout_width="match_parent"
        android:background="@drawable/
        android:layout_height="wrap_content"
        android:orientation="vertical">

答案 3 :(得分:0)

实际上我发布的内容很有效!由于Android Studio设计屏幕上的某些原因,它没有显示。但是,当我在设备上运行它时,它就变了!