【问题标题】:Android ImageView getting over linearlayoutAndroid ImageView 克服线性布局
【发布时间】:2014-06-20 14:10:09
【问题描述】:

我正在开发一个android应用程序,布局如下图所示:

Screenshot http://www.lebanon-trading-center.com/daymanager/layout.PNG

布局包含两个主要的线性布局:一个占设备高度 90% 的水平布局和一个占设备高度 10% 的垂直布局。在水平线性布局内有三个线性布局(两个红色和一个包含图像视图)。每个红色布局是屏幕宽度的 10%,而包含 ImageView 的布局是 80%。这是xml代码:

<?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="vertical"
    android:weightSum="1" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight="0.9"
        android:weightSum="1"
         >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="0.1"
            android:background="#ff0000" >
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:gravity="center_horizontal"
            android:layout_weight="0.8" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:src="@drawable/ic_launcher" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="0.1"
            android:background="#ff0000"  >
        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="0.1"
        android:background="#000000" >
    </LinearLayout>

</LinearLayout>

问题是,当我将 PNG 图像(1036px X 730px)设置为 ImageView 的源时,ImageView 将越过底部的线性布局(黑色的),两个红色布局将被挤压。

Screenshot http://www.lebanon-trading-center.com/daymanager/layout2.PNG

我的问题是如何解决问题并在各种平板电脑上使用相同的布局?

【问题讨论】:

  • 你可以发布一个布局的图像,当你设置 png 图像(1036px X 730px)

标签: java android android-layout


【解决方案1】:
Try this way,hope this will help you to solve your problem.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_weight="0.90">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="0.10"
            android:background="#ff0000" >
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:gravity="center_horizontal"
            android:layout_weight="0.80" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitXY"
                android:src="@drawable/ic_launcher" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="0.10"
            android:background="#ff0000"  >
        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        android:layout_weight="0.10"
        android:background="#000000" >
    </LinearLayout>

</LinearLayout>

【讨论】:

  • 非常感谢它的工作,我可以解决它,使图片不会被拉伸?
  • 当然,把这个:android:scaleType="fitXY"替换成这个:android:adjustViewBounds="true"到ImageView
  • 再次感谢哈雷什!
  • @user3012088 仅供参考,嵌套权重不利于性能。但我认为没有其他选择:)
  • 谢谢,我会考虑的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-22
  • 2020-01-26
  • 2014-11-21
  • 2013-05-19
  • 1970-01-01
  • 2014-03-05
  • 1970-01-01
相关资源
最近更新 更多