【问题标题】:LinearLayout above ImageView not getting displayin Fragment?ImageView 上方的 LinearLayout 没有在 Fragment 中显示?
【发布时间】:2014-07-22 16:39:23
【问题描述】:

我有 ImageView 需要以可用的全高显示。但是在顶部,我创建了包含文本和图像的 LinearLayout。我希望它显示在顶部,但也希望图像适合屏幕。我在底部有线性布局,并且工作正常。

这是我想在顶部显示的 LinearLayout 代码。我隐藏了操作栏

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

        <LinearLayout
            android:id="@+id/actionbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:background="@android:color/black"
            android:gravity="top"
            android:orientation="horizontal"
            android:visibility="visible" >

            <ImageView
                android:id="@+id/backbutton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:paddingLeft="10dp"
                android:src="@drawable/back_button" />

            <TextView
                android:id="@+id/titleofscreen"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Testing"
                android:textColor="#22c064"
                android:textSize="18sp" />
        </LinearLayout>

        <ImageView
            android:id="@+id/imageShown"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:scaleType="fitXY" /> 
</FrameLayout>

如果我没有将 imageView 比例类型设置为 android:scaleType="fitXY" 那么它可以工作,但图像不适合屏幕。

我想显示图像以适合包含文本的上方栏和下方有一些图标的栏。但是,如果我制作图像 scaletype ='fitXY' 它不会显示在文本上方。以下是屏幕截图,但我希望图像占用空间

【问题讨论】:

  • 请发布有关您获得的结果的更多详细信息。 “它不起作用”有点不足以理解你的问题。此外,我不确定您要实现什么 - 是否只是显示填充整个屏幕的图像?顺便提一句。为什么要使用android:scaleType="fitXY"?大多数人关心纵横比,fitXY 不会保持纵横比。也许您应该使用centerCropcenterInside(取决于您想要实现的目标)。

标签: android android-layout


【解决方案1】:

如果您希望图像低于actionbar,则不应使用FrameLayout 作为父级。您应该使用带有权重的垂直LinearLayout 或“RelativeLayout”。尝试以下代码:

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

    <LinearLayout
        android:id="@+id/actionbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="@android:color/black"
        android:gravity="top"
        android:orientation="horizontal"
        android:visibility="visible" >

        <ImageView
            android:id="@+id/backbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:paddingLeft="10dp"
            android:src="@drawable/back_button" />

        <TextView
            android:id="@+id/titleofscreen"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Testing"
            android:textColor="#22c064"
            android:textSize="18sp" />
    </LinearLayout>

    <ImageView
        android:id="@+id/imageShown"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/actionbar"
        android:layout_alignParentBottom="true"
        android:scaleType="fitCenter"
        /> 
</RelativeLayout>


我也怀疑你使用fitXY 作为android:scaleType,因为它不保持图像的纵横比 - 所以你的图像会变形(拉伸)。在您的情况下,您应该使用fitCenter,但这取决于您想要缩放图像的精确程度 - 您也可以使用centerInsidecenterCrop

【讨论】:

  • 没有用 :( 我使用 framelayout 作为我的父级,因为它还有很多其他组件
  • 但是在这个解决方案中什么“不起作用”?我已经对其进行了测试,并且(如果我对您的理解正确)它可以像您希望的那样工作...您可以发布结果吗,至少告诉什么不起作用?或者发布你的代码——你说你不能使用这个解决方案,因为你的代码的其余部分——所以这个“其他代码”对于提出任何好的解决方案非常重要。您不能为此使用FrameLayout,因为您希望您的“操作栏”与此图像相关(您希望操作栏下方的图像),因此您必须使用一些尊重关系的布局(RelativeLayoutLinearLayout
  • 要么添加额外的父级(LinearLayoutRelativeLayout),要么图像与操作栏重叠(这不是你想要的)。您可以使用 FrameLayout 是您的操作栏将具有固定的高度 - 然后您可以在 imageShown 图像上设置 layout_marginTop - 然后它不会显示在操作栏的顶部。但请发布您的代码。顺便提一句。 fitCenter 不适合你(结果是什么)?这是您的主要问题(如何用图像填充屏幕)所以请以某种方式参考它。
  • 这里的人真的很想提供帮助,但如果没有足够的细节,有时会很难。很高兴看到我可以提供帮助:) 如果此答案解决了您的问题,您应该接受它(这将帮助其他用户找到解决他们问题的方法)并且如果它对您也有某种用途,您可以投票:)跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多