简单的Android布局不符合预期

时间:2013-12-24 18:51:11

标签: android

我想要一个这样的布局(边框代表Android设备的屏幕):

enter image description here

也就是说,我希望,在一个专栏中:

  1. 标题
  2. 在此之下,一个图像,缩放到恰好适合屏幕的宽度。
  3. 紧接着,日期和说明。
  4. 如果需要,整个事物可以垂直滚动(即,如果图像非常高)。

    我正在尝试线性布局,图像的宽度/高度分别设置为“match_parent”和“wrap_content”,但是我得到了这种奇怪的行为,其中图像上方和下方有大量的屏幕填充边距,强制日期和描述超出屏幕底部。

    这是一个非常简单的布局。实施它的正确方法是什么?

    我的尝试看起来像这样:

    <ScrollView 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"
    android:background="#000000"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:textColor="#FFFFFF"
    tools:context=".MainActivity" >
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:textColor="#FFFFFF"
        >
    
        <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title"
        android:textSize="40sp" />
    
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/iotd" />
    
        <TextView
            android:id="@+id/description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Date"
            android:textSize="20sp" />
    
        <TextView
            android:id="@+id/description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Description"
            android:textSize="20sp" />
    </LinearLayout>
    
    </ScrollView>
    

3 个答案:

答案 0 :(得分:0)

<ScrollView 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"
android:background="#000000"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:textColor="#FFFFFF"
tools:context=".MainActivity" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:textColor="#FFFFFF"
>

<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:textSize="40sp" />

<ImageView
    android:id="@+id/imageView1"
    android:scaleType="center"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/iotd" />

<TextView
    android:id="@+id/description"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Date"
    android:textSize="20sp" />

<TextView
    android:id="@+id/description"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Description"
    android:textSize="20sp" />

答案 1 :(得分:0)

尝试为您的imageview设置scaleType。

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scaleType="centerCrop"
    android:src="@drawable/iotd" />

有关各种scaleTypes的详细信息,请参阅 http://developer.android.com/reference/android/widget/ImageView.ScaleType.html

CenterCrop将均匀缩放图像(保持图像的纵横比),使图像的尺寸(宽度和高度)等于或大于视图的相应尺寸(减去填充)。

答案 2 :(得分:0)

我猜您正在寻找android:adjustViewBounds="true"。与您想要的scaleType一起使用。