Android布局;图像占据整个屏幕
Android Layout; Image takes entire screen
我正在尝试用两个视图填充一个 activity,一个 ImageView
在左侧,一个 TextView
在右侧。图像的大小应能填满整个垂直 space。文本应占据 space 剩下的任何内容。看了几个教程,好像我需要的是水平方向的LinearLayout
,TextView
的layout_weight
属性是正整数。
这是我目前拥有的:
<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>
但是,问题是图像在左侧和右侧创建了白色 space,从而填满了整个屏幕。 TextView
没有剩余空间。
我还尝试使用 RelativeLayout
并将 ImageView
的 toStartOf
属性 设置为 TextView
的 ID,但随后的文本而是填满整个屏幕。
当我开始做这个时,我没想到这会是一件如此复杂的事情activity,但我们做到了。任何帮助将不胜感激。
将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" />
您可以使用 ImageView scaleType,例如:
android:scaleType="fitCenter"
我认为您缺少的是 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 占用的空间 space 多于显示图像所需的空间。
上面的布局在 Android Studio 中创建了一个预览,对我来说是这样的:
我正在尝试用两个视图填充一个 activity,一个 ImageView
在左侧,一个 TextView
在右侧。图像的大小应能填满整个垂直 space。文本应占据 space 剩下的任何内容。看了几个教程,好像我需要的是水平方向的LinearLayout
,TextView
的layout_weight
属性是正整数。
这是我目前拥有的:
<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>
但是,问题是图像在左侧和右侧创建了白色 space,从而填满了整个屏幕。 TextView
没有剩余空间。
我还尝试使用 RelativeLayout
并将 ImageView
的 toStartOf
属性 设置为 TextView
的 ID,但随后的文本而是填满整个屏幕。
当我开始做这个时,我没想到这会是一件如此复杂的事情activity,但我们做到了。任何帮助将不胜感激。
将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" />
您可以使用 ImageView scaleType,例如:
android:scaleType="fitCenter"
我认为您缺少的是 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 占用的空间 space 多于显示图像所需的空间。
上面的布局在 Android Studio 中创建了一个预览,对我来说是这样的: