【问题标题】:Android inflated view not shown if larger than screen如果大于屏幕,则不显示 Android 膨胀视图
【发布时间】:2015-10-15 17:59:44
【问题描述】:

我有一个带有 RelativeLayout 的 Horizo​​ntalScrollView 和另一个我添加动态视图的相对布局。 如果我添加使用布局充气器创建的视图并且该视图大于屏幕,则不会显示。

例如:

 View view1 = layoutInflater.inflate(R.layout.some_linear_layout, null, false);
    RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(6000, 50);
    lp1.leftMargin =500;
    lp1.topMargin = 20;
    view1.setLayoutParams(lp1);
    parentRelativeLayout.addView(view1);

    View view2 =  new LinearLayout(getActivity());
    RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(6000, 50);
    lp2.leftMargin =500;
    lp2.topMargin = 20;
    view2.setLayoutParams(lp2);
    parentRelativeLayout.addView(view2);

在这种情况下,只有 view2 可见,而 view1 不可见。 知道如何让 view1 出现吗?

父布局xml类似于:

  <HorizontalScrollView
   android:id="@+id/scrollview"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
         <RelativeLayout
             android:id="@+id/panel1"
             android:layout_width="wrap_content"
             android:layout_height="match_parent"
             android:minWidth="200dp">
                 <RelativeLayout
                    android:id="@+id/panel2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_alignParentBottom="true"
                    android:background="@color/black">
     </.......

在添加子元素之前,我将 panel1 和 panel2 的宽度设置为 8000px

【问题讨论】:

  • 似乎view2view1 重叠。将layout_toRightOf 添加到view2 使其位于view1 的右侧。
  • 我希望它们重叠。问题不在于重叠,如果宽度大于屏幕,view1 根本不会出现。我在示例中添加 2 是为了显示视图 2 出现而视图 1 不出现,即使我只留下其中一个并评论另一个
  • 我还应某人的要求添加了父布局

标签: android android-relativelayout horizontal-scrolling


【解决方案1】:

显然是我的错

我将背景定义为

<corners
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp" />

如果视图小于屏幕宽度,则有效

将其更改为(下),现在出现背景

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/red" />
<corners android:radius="10dp" />
</shape>

【讨论】:

    猜你喜欢
    • 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
    相关资源
    最近更新 更多