我正在制作带有材质卡及其阴影的气象应用程序。 我找到了一种使用线性布局将它们居中的方法,但是它可以消除阴影。 我该如何预防?是否可以在不使用线性布局或框架布局的情况下对齐它们?
这是我的布局代码: 我以一个FrameLayout为根,我的LinearLayout包含两个物料卡,我只是想将它们作为一个组居中,如果您知道另一种方法,请告诉我!
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/img_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/background"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:fontFamily="cursive"
android:layout_gravity="center_horizontal"
android:layout_marginTop="25dp"
android:textSize="50sp"
android:textColor="#FFF"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your location's weather in a touch!"
android:layout_gravity="center_horizontal"
android:layout_marginTop="90dp"
android:textSize="17.3sp"
android:textColor="@color/colorPrimaryText"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center">
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="270dp"
android:layout_height="140dp"
android:layout_gravity="center_horizontal"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="5dp">
<TextView
android:id="@+id/tv_temperature"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:padding="16dp"
android:text="30°C"
android:textColor="@color/colorPrimaryText"
android:textSize="50sp" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cv_data"
android:layout_width="270dp"
android:layout_height="140dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="5dp">
<TextView
android:id="@+id/tv_conditions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:padding="16dp"
android:text="Scatted Clouds"
android:textColor="@color/colorPrimaryText"
android:textSize="30sp" />
</android.support.v7.widget.CardView>
</LinearLayout>
<ProgressBar
android:id="@+id/pb_loading"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_gravity="center"
android:elevation="20dp"/>
<TextView
android:id="@+id/tv_city_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="center"
android:padding="20dp"
android:fontFamily="sans-serif"
android:textColor="#FFF"
android:textSize="20sp"/>
答案 0 :(得分:4)
看起来您的布局会比ConstraintLayout
更好,而不是FrameLayout
,但我们仍然可以使您的工作得到满足。
问题是(在较新的Android版本上)CardView
的阴影实际上是在视图边界的外部绘制的。通常,这很好,但是如果CardView
在父级内部,则如果父级没有足够的空间显示阴影,则该父级可以裁剪阴影。
对于您而言,“轻松修复”实际上非常容易。将LinearLayout
的宽度更改为match_parent
;这将为纸牌留出阴影的空间。
我上面说的将解决每张卡侧面的阴影,但不会解决顶部卡上方或底部卡下方的阴影。再次,本着快速修复的精神,建议将这些属性添加到LinearLayout中:
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:clipToPadding="false"
答案 1 :(得分:1)
您需要更改height
并将更改layout_gravity
添加到gravity
,如以下代码所示:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical">
它可能会解决您的问题=]
如果您想改善代码,建议您不要设置固定的宽度和高度,这会给某些设备带来麻烦。您也只能在您的xmlns:card_view
上加载一次LinearLayout
,这是您的第一个标签