2016-02-07 35 views
0

我正尝试用两个视图填充活动,左侧为ImageView,右侧为TextView。图像的大小应该填满整个垂直空间。文本应该占用剩下的空间。看了几个教程后,似乎我需要的是一个水平方向的LinearLayout,并且TextViewlayout_weight属性是一个正整数。Android布局;图片占用整个屏幕

这是我到目前为止有:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:src="@drawable/charsheet" /> 

    <TextView 
     android:id="@+id/ipsum" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:text="@string/ipsum" /> 

</LinearLayout> 

的问题,但是,图像,创建空白到填满整个屏幕的左侧和右侧。 TextView没有剩余空间。

enter image description here

我也尝试使用RelativeLayoutImageViewtoStartOf属性设置为TextView的ID,但随后的文字填满整个屏幕来代替。

当我开始进行这项活动时,我不会认为这会是一件很复杂的事情,但我们在这里。任何帮助,将不胜感激。

+1

你想要的形象被裁剪,以适应或调整大小以适应? –

+0

调整大小,我想。我想看到整个图像,只是减去正在添加的空白空间 –

回答

3

我认为你缺少的东西是对的ImageView adjustViewBounds。像这样尝试:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    > 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:scaleType="fitStart" 
     android:adjustViewBounds="true" 
     android:src="@drawable/doug" 
     /> 

    <TextView 
     android:id="@+id/ipsum" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:text="ipsum" 
     /> 

</LinearLayout> 

如果没有adjustImageBounds,ImageView会占用比显示图像所需的更多空间。

上述布局创建Android Studio中的预览,看起来像这样对我说:

enter image description here

+0

它不适用于Android studio 3.0。我只是试了一下。背景是黑色的,textview更小。 –

0

android:layout_weight="1"移动到ImageView,并在TextView中使权重为0.注意:您需要将ImageView的layout_width设置为0px。

<ImageView 
    android:id="@+id/imageView" 
    android:layout_weight="1" 
    android:layout_width="0px" 
    android:layout_height="match_parent" 
    android:src="@drawable/ic_launcher" /> 

<TextView 
    android:id="@+id/ipsum" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:text="ipsum" /> 
+0

这会导致文本完全填满屏幕,导致图像隐藏 –

+0

我忘了layout_width:0px,现在尝试 – Gavriel

+0

现在图像不会缩放,整个屏幕充满了图像的一部分。 –

0

你可以用ImageView的scaleType玩,如:

android:scaleType="fitCenter" 
+0

这似乎没有任何效果 –