【问题标题】:Android: Setting a fixed Height for Layout?Android:为布局设置固定高度?
【发布时间】:2015-01-08 15:27:50
【问题描述】:

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

到目前为止我所做的:

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

【问题讨论】:

  • 50dp 在任何设备上仍然是 50dp,它看起来很小,因为屏幕更大。如果你想为不同的屏幕分辨率设置不同的尺寸,你需要使用那些特定的values文件夹分辨率并设置特定的值
  • 为什么不尝试使用 layoutWeight?您可以这样做,即使您需要明确设置项目的高度,您也可以按照@tyczj 所说的进行操作,并拥有一个值文件夹来处理不同的设备。

标签: java android android-layout mobile cardlayout


【解决方案1】:

一张卡片要显示四个元素

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

由于标题和描述在某些点上与图像重叠,我建议您使用RelativeLayout 并设置

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

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-25
  • 1970-01-01
  • 2012-01-23
  • 1970-01-01
  • 2012-08-31
  • 2013-01-15
相关资源
最近更新 更多