【问题标题】:Rounded corners on linearlayout [closed]线性布局上的圆角[关闭]
【发布时间】:2019-05-01 04:08:43
【问题描述】:

我有一个带有 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。这不起作用,因为儿童背景颜色将接管透明度并保持锐利的边缘

【问题讨论】:

  • 如果您将预期的输出分享为图像,那就太好了
  • 为什么不在线性布局和子布局中使用可绘制形状?

标签: android android-layout


【解决方案1】:

res/drawable/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>

activity.xml

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

【讨论】:

  • 这么简单的答案,对我有用。
【解决方案2】:

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

<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
【解决方案3】:

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

既然LinearLayout 覆盖了所有CardView,那么整个视图就是 CardView
因此,不要将透明背景设置为CardView,而是将其设置为LinearLayout,将背景颜色设置为CardView,您将看到圆角。
如果您将CardViewLinearLayout 都设置为透明颜色,那么您如何期望看到圆角,因为没有角

【讨论】:

  • 我希望孩子们(它们是带有背景颜色的 Textview)在与 cardview 角相交的地方有圆角
  • 如果您提供 0 边距,顶部的 TextView 将仅圆角其顶角,而底部仅圆角其底角。您不能仅仅因为父视图具有圆角而期望子视图具有圆角。
  • 是的,我正在寻找 Top textview 只圆角它的顶角和底部只有它的底角。通过使父视图圆角,我希望它会夹住孩子的角。顺便说一句,我做到了。我添加了为什么它似乎不起作用的答案
  • 那么你所要做的就是为顶部TextView设置上边距0dp,为底部TextView设置下边距0dp。
  • 无需设置 0 边距。我上面提供的内容显然有效,但不适用于 Android Studio 设计屏幕,仅适用于设备。我会接受最接近“什么都不做,按原样工作”的答案
猜你喜欢
  • 2016-01-06
  • 2014-01-25
  • 1970-01-01
  • 1970-01-01
  • 2012-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-17
相关资源
最近更新 更多