图像超出图像视图范围

时间:2011-12-16 22:41:51

标签: android android-layout

我无法弄清楚为什么我的图像不在我的imageview范围内。相反,它向左浮动并隐藏。这只是因为图形界面没有显示它吗?

编辑:

我编辑了原始代码,以便更清楚地显示我遇到的问题并添加了一张图片(我希望图片显示在红色框中):

enter image description here

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 
    android:id="@+id/top_container"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"     
    android:gravity="center"
    >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        >
        <View
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="195"
            android:background="#00FF00"/>  
        <ImageView
            android:id="@+id/img"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#ff0000"
            android:src="@drawable/img" />   

        <View
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:background="#0000FF"
        />  
    </LinearLayout>
</LinearLayout>

3 个答案:

答案 0 :(得分:0)

为什么要将android:layout_height设置为0dp?除非我将其更改为fill_parent

,否则我甚至无法显示它

无论哪种方式,都是因为您layout_weight为父LinearLayout。为layout_weight指定一个更大的ImageView,它将会显示在其中。

    <ImageView
        android:id="@+id/my_img"
        android:gravity="center"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="279"
        android:src="@drawable/my_img" />

这对我有用..但数学可能不对。

答案 1 :(得分:0)

我不知道从哪里开始解决你发布的问题,在进一步调试之前进行一些简单的观察:

  • 您的布局所有需要ID(例如android:id =&#34; @ + id / another_layout&#34;)
  • 第二个线性布局为0dp
  • 的宽度
  • 您的第一个线性布局的高度为0 dp,结合之前的I&#39;令人惊讶的是它呈现
  • 最后一个内部线性布局的宽度为0dp

你想要达到什么目的?对我来说似乎有很多布局,除非你删除了一些元素以使其更容易理解?你可以发布一个你想要做的事情的简单图像,然后我们可以帮你改进标记吗?

答案 2 :(得分:0)

好的,我有它的工作。 “block_container”的height属性设置为随机200dp,您希望将高度更改为您需要的任何高度,或者可能将其设置为“wrap_content”。我在模拟器和设备上测试了这个。

我还假设您希望所有三个块均匀间隔。注意父“block_container”的weight_sum如何为9?孩子们的宽度是相等的,因为它们的重量分别为3(3块* 3重量= 9重量)。

我注意到你之前看起来像是试图直接使用重量作为宽度,例如重量为569.记住重量!=直接宽度。

编辑:从一些视图中添加了缺少的id属性

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/top_container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <LinearLayout
        android:id="@+id/block_container"
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:orientation="horizontal"
        android:weightSum="9"
        >
        <View
            android:id="@+id/left_block"
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="3"
            android:background="#00FF00"/>  
        <ImageView
            android:id="@+id/img"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="3"
            android:background="#ff0000"
            android:src="@drawable/logo" />   

        <View
            android:id="@+id/right_block"
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="3"
            android:background="#0000FF"
        />  
    </LinearLayout>
</LinearLayout>