java - Android:为布局设置固定高度?

标签 java android android-layout mobile cardlayout

我正在尝试制作以下“卡片”布局,它显示图像和布局中包含的所有内容如何设置为固定高度,模仿现实生活中的水平卡片。

enter image description here

到目前为止我做了什么:

以下 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 在平板电脑上看起来会很小吗?我错了吗?

最佳答案

一张卡片有四个元素要显示

  1. 图片
  2. “V”图标
  3. 标题(白色文字)
  4. 描述(灰色文本)

因为标题和描述在某些地方与图像重叠,所以我建议您使用 RelativeLayout 并设置

  1. 卡片在运行时的高度取决于设备的屏幕宽度,因此它在所有设备上看起来都相似。

  2. 为不同类型的设备使用 Values 文件夹,并从那里设置以 dp 为单位的大小。

关于java - Android:为布局设置固定高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27843641/

相关文章:

java - 如何使用 JavaFX 图表 API 生成图表图像以进行导出而无需先显示

android - 在某些 Android 设备(版本 4+)中播放视频时出现问题,但在其他设备中则不会

android - TextView 内容在 android 4.2.2 上不正确居中

android - 自定义方形布局未按预期工作

android - 按钮不水平居中

Java使用extends访问变量

java - 使用 OKHTTP3 上传带有进度条的多个文件

java - 根据 jTextField 值采取不同的操作

android - Google Play 服务在 Android 模拟器中已过时

java - 尝试更新到 SDK 工具 26.1.1 时出现问题