【发布时间】:2016-04-01 17:51:59
【问题描述】:
我得到以下代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
android:weightSum="1">
<ImageView
android:id="@+id/slider_image"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.6"/>
<TextView
style="@style/DefaultText"
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.4"/>
(因为我还不知道图片的大小,所以我必须通过layout_weight来设置ImageView的大小。)
代码 sn -p 的结果是:
|-------------------|
| | | |
| | IMAGE | |
| | | |
|-------------------|
| |
| <<< TEXT >>> |
| |
|-------------------|
我想得到什么:
|-------------------|
| | | |
| | IMAGE | |
| | | |
|-------------------|
| | | |
| | TEXT | |
| | | |
|-------------------|
我不确定它是否可以在纯 XML 中使用,或者我是否必须为此使用 Java。
无论如何-我不能使用RelativeLayout的layout_alignLeft和layout_alignRight,因为我必须先设置ImageView的高度。
任何建议将不胜感激!
【问题讨论】:
-
布局中的
ImageVIew占据了父LinearLayout的整个宽度,不管那是什么。如果图像内容没有填满整个宽度,它可能不会这样显示,但视图本身仍然与父视图一样宽。 -
这是真的,但在这种情况下,有没有办法将父母的宽度设置为等于 ImageView 的宽度?还是我通常有错误的方法?
-
我想不出任何只使用框架提供的布局的东西。
-
似乎我必须在活动 onCreate() 中以编程方式设置宽度
标签: android xml android-layout layout android-linearlayout