【问题标题】:Huge gap between imageview and textviewimageview 和 textview 之间的巨大差距
【发布时间】:2016-03-08 14:55:05
【问题描述】:

在我的片段活动中,我有一些页面有图像,而有些则没有。布局是这样的:Textview-Imageview-Textview,所以当我没有图像时,我希望 Textviews 在彼此下方。这是我的 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#006400">

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="match_parent">
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/handling"
        android:textColor="#ffffff"
        android:textSize="20dp" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/stilling"
        android:layout_below="@id/handling"
        android:layout_gravity="center_vertical" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:textAlignment="center"
                android:text="New Text"
                android:id="@+id/arabisk"
                android:layout_below="@id/stilling"
                android:layout_gravity="center"
                android:textSize="18dp"
                android:textColor="#ff0000" />

        </RelativeLayout>

    </ScrollView>

</LinearLayout>

This is what I get

编辑:

我的图片数组

int[] billede = new int[18];
        billede[1] = R.mipmap.sojle1;
        billede[2] = R.mipmap.sojle4;
        billede[3] = R.mipmap.sojle2;
        billede[4] = R.mipmap.sojle3;
        billede[6] = R.mipmap.sojle4;
        billede[8] = R.mipmap.sojle6;
        billede[10] = R.mipmap.sojle7;
        billede[12] = R.mipmap.sojle8;
        billede[15] = R.mipmap.sojle9;

        stilling.setImageResource(billede[mPage]);

        return view;

【问题讨论】:

  • 好吧,你为图像视图设置了一个高度,那么你期望什么。如果没有图像,则将可见性设置为 GONE
  • 如何在我的阵列中做到这一点? (已上传)我不会写billede[13] = stilling.GONE
  • 您将图像视图设置为消失而不是位图
  • 我试过了android:visibility="gone",但这会删除所有图片
  • 好吧,因为您将可见性设置为消失,如果您正在循环浏览图像并显示它们,您需要添加逻辑以仅在有图像时显示图像

标签: android android-layout android-imageview textview


【解决方案1】:

将可见性设置为GONE

在你的 xml 中

android:visibility="GONE"

或通过代码

imageView.setVisibility(View.GONE);

【讨论】:

  • @Hudhud 因为它不见了?如果您通过代码设置 src,则必须再次将可见性设置为 View.VISIBLE。
  • 我该怎么做?我不能说 if(billede[mPage] != null){ stilling.setVisibility(View.VISIBLE); stilling.setImageResource(billede[mPage]); } 作为运算符 != 不能应用于 null/int
  • @Hudhud 除了您尝试将原始类型与对象进行比较之外,您为什么还要检查呢?一旦你设置了一个图像 src,让它可见........
  • 已解决问题。是!=0
【解决方案2】:

将 ImageView 的可见性设置为 Gone,并且您的文本视图必须像您想要的那样低于其他视图。像这样

stilling.setVisibility(View.GONE);

【讨论】:

    【解决方案3】:

    使用 LinearLayout 而不是 RelativeLayout。并设置可见 View.GONE 隐藏ImageView。

    【讨论】:

      猜你喜欢
      • 2021-11-26
      • 1970-01-01
      • 2020-01-11
      • 1970-01-01
      • 2021-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多