【问题标题】:Android-layout does not work properlyAndroid 布局无法正常工作
【发布时间】:2012-02-22 15:15:41
【问题描述】:

我正在使用文件 main.xml 在 Android 中设计 UI。我不知道为什么最后一个 TextView (id: ImageDescription) 不起作用。如果我删除ImageView 标签,最后一个TextView 将再次起作用。 这是我的截图。第一个在没有ImageView 标签时,第二个在有ImageView

如您所见,当有图像时,我看不到Image descriptor: a cartoon tiger 行。 这是我的main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/imageTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/test_image_title"/>

     <TextView 
        android:id="@+id/imageDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/test_image_date"/>


     <ImageView
         android:id="@+id/imageDisplay"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:contentDescription="@string/test_contentDescription"
         android:src="@drawable/test_image"/>

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TextView 
            android:id="@+id/imageDescription"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/test_image_description"/>
    </ScrollView>         

</LinearLayout>

谢谢你的帮助:)

【问题讨论】:

  • 来自 ScrollView 文档The TextView class also takes care of its own scrolling, so does not require a ScrollView
  • @Selvin 是的。我认为问题不在于ScrollView,因为当我删除ScrollView 标签时,仍然没有什么不同:(
  • 它非常简单明了...图像太大...顶部和底部似乎有黑条...也许您应该将图像和描述都放在另一个线性布局然后把这个线性布局放到scrollview ...

标签: android android-layout ui-design


【解决方案1】:

您正在将您的 imageDescription textview 添加到 imageview 下方,并且您的滚动视图也同时具有 with 和 hight 作为 fill_parent,一旦尝试这个

 <TextView 
        android:id="@+id/imageDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/test_image_date"/>


    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView 
            android:id="@+id/imageDescription"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/test_image_description"/>
    </ScrollView> 

     <ImageView
         android:id="@+id/imageDisplay"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:contentDescription="@string/test_contentDescription"
         android:src="@drawable/test_image"/>

我认为图像太大,如果是这种情况,请尝试以适当的百分比为图像和文本视图指定 layout:weight 参数。

【讨论】:

  • 是的。你的代码可以工作,但这不是我想要的 UI。我想要下面的图像描述。我可以吗?
  • 那么,只更改滚动视图的宽度和高度,就像我的帖子中那样,因为它试图占据父级的总大小。
  • 我认为图像太大,如果是这种情况,请尝试以适当的百分比为图像和文本视图指定 layout:weight 参数。
  • 哦。我没有看到您在原始帖子中添加 layout:weight :) 谢谢 :)
【解决方案2】:

尝试更改您的 XML 部分

<ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

<ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

因为您的ScrollView 试图在高度上与您的LinearLayout 一样大。

【讨论】:

  • 另外,您的 ImageView 的 XML 中没有任何边距或填充,但它位于您的 TextViews 之下。您的 testimage 顶部和底部是否没有黑色或透明条纹将 ScrollView 推到可视区域之外?
  • 是的。也许我的图像有透明的肚带,但是当我把它作为墙纸时,我在上下两侧都看不到透明的肚带。怎么测试呢,知道的请教教我:)
  • 好吧,这对你来说更容易: 1:在你最喜欢的图像操作器上检查图像在PC上。 2:尝试将 ImageView 的宽度和/或高度设置为一些静态值,例如“20sp”或“30dp” 3:尝试另一个图像,不要忘记使用“wrap_content”
【解决方案3】:

我的问题有另一个答案,我在这里发帖给谁看过我的帖子并需要更多答案。我使用android:adjustViewBounds="true" 属性:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-17
    • 2012-10-10
    • 2018-04-02
    • 1970-01-01
    • 2017-08-03
    • 2012-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多