我正在尝试制作以下“卡片”布局,它显示图像和布局中包含的所有内容如何设置为固定高度,模仿现实生活中的水平卡片。
到目前为止我做了什么:
以下 XML 是我膨胀到 GridView 适配器中的单个网格项;这是上面提到的“卡片”之一。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginLeft="@dimen/feed_item_margin"
android:layout_marginRight="@dimen/feed_item_margin"
android:background="@drawable/bg_card"
android:orientation="vertical"
android:paddingBottom="@dimen/feed_item_padding_top_bottom"
android:paddingTop="@dimen/feed_item_padding_top_bottom" >
<ImageView
android:id="@+id/grid_banner"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:background="@drawable/ic_launcher"/>
<TextView
android:id="@+id/bannerTitle"
android:layout_width="285dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/grid_image"
android:layout_alignLeft="@id/grid_image"
android:layout_alignRight="@id/grid_image"
android:layout_alignTop="@id/grid_image"
android:layout_marginLeft="15sp"
android:layout_marginTop="300sp"
android:shadowColor="#000000"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="5.0"
android:textColor="#FFFFFF"
android:textSize="25sp" />
</LinearLayout>
</LinearLayout>
我知道我可以为根 LinearLayout 设置一个固定值,但这会影响通过不同的移动设备查看吗?比如,50dp 在平板电脑上看起来会很小吗?我错了吗?
最佳答案
一张卡片有四个元素要显示
- 图片
- “V”图标
- 标题(白色文字)
- 描述(灰色文本)
因为标题和描述在某些地方与图像重叠,所以我建议您使用 RelativeLayout
并设置
卡片在运行时的高度取决于设备的屏幕宽度,因此它在所有设备上看起来都相似。
为不同类型的设备使用 Values 文件夹,并从那里设置以 dp 为单位的大小。
关于java - Android:为布局设置固定高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27843641/