2015-01-08 157 views
1

我试图做出如下'卡'布局,其中显示了图像和布局中包含的所有内容如何设置到一个固定的高度,它模仿一个真实的生活水平卡。Android:为布局设置固定高度?

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

+0

50dp在任何设备上仍然是50dp,它看起来很小,因为屏幕比较大。如果你想为不同的屏幕分辨率使用不同的尺寸,你需要使用这些特定的'values'文件夹分辨率并设置具体的值 – tyczj

+0

为什么不尝试使用layoutWeight?你可以这样做,即使你需要明确地设置一个项目的高度,你也可以像@tyczj所说的那样做,并且有一个值文件夹来处理不同的设备。 – sparkyShorts

回答

2

你有四个要素,以示对一卡

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

因为标题和描述在某些点重叠图像,所以我建议你使用RelativeLayout并且还设置

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

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

+0

请问您在设置尺寸值时是否有任何示例代码? :)并通过设置运行时的高度,你的意思是onCreate? – Pjayness

+1

这将是有益的http://stackoverflow.com/questions/22458468/set-layout-params-dynamically – Rohit5k2