设置的ImageView的最大宽度为其父的宽度的百分比 [英] Set ImageView's max width as a percent of its parent's width

查看:303
本文介绍了设置的ImageView的最大宽度为其父的宽度的百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从一个网站,其中每个项目都有标题,描述和图片加载导航。我想有所有沿同一轴线为中心的标题和描述。

I'm loading navigation from a website, where each item has a title, description, and image. I want to have all of the titles and descriptions centered along the same axis.

我使用了在ListView每个项目下面的布局。它工作正常时,图像的一半宽,因为他们身材高大,但如果他们是任何更广泛 - 图片下的文字去

I'm using the following layout for each item in a ListView. It works fine when the images are half as wide as they are tall, but not if they are any wider - the text goes under the image.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:orientation="horizontal">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1">
            <TextView
                android:layout_height="wrap_content"
                android:textSize="10pt"
                android:id="@+id/title"
                android:gravity="center_horizontal"
                android:layout_width="fill_parent" />
            <TextView
                android:layout_height="wrap_content"
                android:id="@+id/description"
                android:layout_width="fill_parent"
                android:gravity="center_horizontal" />
        </LinearLayout>
        <View
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            android:layout_weight="3" />
    </LinearLayout>
    <ImageView
        android:id="@+id/icon"
        android:adjustViewBounds="true"
        android:layout_width="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_height="wrap_content" />
</RelativeLayout>

是否有可能迫使形象留在RelativeLayout的宽度还是少了?

Is it possible to force the image to stay at 25% of the RelativeLayout's width or less?

推荐答案

这不能在XML完成。你必须指定在code的大小。我会做,在的onCreate()的活动:

This can't be done in the XML. You would have to specify the size in the code. I would do that in the onCreate() of the activity:

【编辑:增加进口,浮动符号]

import android.widget.LinearLayout.LayoutParams;
...
obj.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                                     LayoutParams.WRAP_CONTENT,
                                     0.25f));

此方法的使用100%的家长的高度,但在父母的宽度的25%的。

这篇关于设置的ImageView的最大宽度为其父的宽度的百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆