【问题标题】:The content of a LinearLayout isn't visibleLinearLayout 的内容不可见
【发布时间】:2013-04-22 16:33:05
【问题描述】:

我尝试以编程方式将一些 GUI 元素(如 ImageView 或 TextView)添加到 LinearLayout。但是元素没有显示出来。

为了查看一个元素是否被绘制,我为每个元素设置了不同的背景颜色。结果我只能看到LinearLayout的背景色。但为什么呢?

public class MyLinearLayout extends LinearLayout {
  public MyLinearLayout(Context context) {
    super(context);

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    setLayoutParams(params);
    setBackgroundColor(Color.RED);


    imageView = new ImageView(context);
    params = new LinearLayout.LayoutParams(100, 100);
    imageView.setLayoutParams(params);
    imageView.setBackgroundColor(Color.BLUE);

    addView(imageView);
  }
}

奇怪的是,我可以看到 LinearLayout 的红色背景颜色,但在 ImageView 的大小。如果我添加一些其他 GUI 元素,如 TextView,我可以看到 LinearLayout 是如何增长的。但我看不到 TextView。

我真的很困惑,因为这不是我第一次做这样的事情。你能告诉我我做错了什么吗?


这是 layout.xml 文件的 sn-p:

<LinearLayout android:layout_width="match_parent"
                  android:layout_height="45dp"
                  android:id="@+id/bottom_bar"
                  android:layout_alignParentBottom="true"
                  android:gravity="bottom">

        <FrameLayout android:id="@+id/block_edit_delete_layout"
                     android:layout_height="match_parent"
                     android:layout_width="wrap_content"
                     android:background="@drawable/block_edit_delete_selector">

            <ImageView android:layout_height="match_parent"
                       android:layout_width="wrap_content"
                       android:src="@drawable/block_edit_delete"
                       android:scaleType="fitXY"
                       android:contentDescription="@string/delete"/>
        </FrameLayout>

        <LinearLayout
                android:id="@+id/block_edit_progress"
                android:layout_height="match_parent"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="horizontal"/>

        <FrameLayout android:id="@+id/block_edit_random_layout"
                     android:layout_height="match_parent"
                     android:layout_width="wrap_content"
                     android:background="@drawable/block_edit_delete_selector">

            <ImageView android:layout_height="match_parent"
                       android:layout_width="wrap_content"
                       android:src="@drawable/block_edit_random"
                       android:scaleType="fitXY"
                       android:contentDescription="@string/random_numbers"/>

        </FrameLayout>
    </LinearLayout>

ID为block_edit_progress的LinearLayout是MyLinearLayout类的多个实例的容器布局。在代码中添加实例:

    for(int i = 0; i < numberOfMyLinearLayouts; i++) {
        MyLinearLayout v = new MyLinearLayout(getContext());
        addView(v);
    }

我希望这会有所帮助。

【问题讨论】:

  • 您的 LinearLayout 高度和宽度设置为 wrap_content。您还没有添加任何要查看的文本视图

标签: android android-linearlayout android-imageview invisible


【解决方案1】:

如果我将您的代码转换为 xml,它会是这样的:

    <LinearLayout layout_width=wrap_content, layout_height = wrap_content>
    <LinearLayout id= MyLinearLayout>//just an idea, syntax may be wrong
    <LinearLayout layout_width= 100, layout_width=100>
    <ImageView color=BLUE>
    </ImageView>
    </LinearLayout>
    </LinearLayout>
    </LinearLayout>

每当您在视图上调用 setLayoutParams 时,您提供的参数 params 应该是父元素。

如果您希望线性布局成为您的线性布局的父级,请尝试使用 MATCH_PARENT 作为宽度、高度,如果您希望视图跨越视图父级的宽度、高度

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
             ViewGroup.LayoutParams.MATCH_PARENT,
             ViewGroup.LayoutParams.MATCH_PARENT);
setLayoutParams(lp);//lp is parent view

也试试这个,以防万一视图被添加到您的视图右侧,而您无法在屏幕上看到它们

yourview.setOrientation(LinearLayout.VERTICAL);

【讨论】:

    【解决方案2】:

    将线性布局的宽高改为match_parent,看看变化如何。 wrap_content 只会显示线性布局的内容,这似乎是你的问题。

    【讨论】:

      【解决方案3】:

      我解决了这个问题。 (或找到解决方法)

      我将完整的初始化内容移出 MyLinearLayout 的构造函数。如果我在布局完全生成后添加一个视图,一切正常。

      像这样:

      MyLinearLayout ll = new MyLinearLayout(getContext());
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100, 100);
      ll.setLayoutParams(params);
      ll.setBackgroundColor(Color.RED);
      
      ImageView v = new ImageView(getContext());
      params = new LinearLayout.LayoutParams(50, 50);
      v.setLayoutParams(params);
      v.setBackgroundColor(Color.GREEN);
      
      ll.addView(v);
      addView(ll);
      

      我不知道为什么其他方式不起作用。感谢您的快速答复!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多