【问题标题】:Scroll view is not working properly when adding through java code通过java代码添加时滚动视图无法正常工作
【发布时间】:2017-04-22 10:31:52
【问题描述】:
RelativeLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    layout = new RelativeLayout(this);
    setContentView(layout);

    ScrollView sc = new ScrollView(this);
    sc.setBackgroundColor(Color.YELLOW);
    sc.setLayoutParams(layoutParams(300, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.ALIGN_PARENT_RIGHT));

    RelativeLayout rl = new RelativeLayout(this);
    rl.setBackgroundColor(Color.CYAN);
    rl.setLayoutParams(layoutParams(300, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.ALIGN_PARENT_RIGHT));


    int k=0;
    for(int i=0;i<15;i++){
        ImageView iv1 = new ImageView(this);
        iv1.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));
        iv1.setLayoutParams(layoutParams(200, 200,0));
        iv1.setTranslationY(k);
        k+=200;
        rl.addView(iv1);
    }
    sc.addView(rl);
    layout.addView(sc);
}

public RelativeLayout.LayoutParams layoutParams(int width,int height,int rule1){
    RelativeLayout.LayoutParams lpb = new RelativeLayout.LayoutParams(width,height);
    if(rule1 != 0){
        lpb.addRule(rule1);
    }
    return lpb;
}

Output of code

当我使用没有 ScrollView 的 RelativeLayout 时,它会显示所有图像,但是当我使用 ScrollView 时,它不会显示所有图像。
请检查代码的问题在哪里。

它应该在 ScrollView 内右对齐。 Desired output

所需输出的 ​​XML 代码,我正在尝试将其转换为 java 代码。

<ScrollView
    android:layout_width="300px"
    android:layout_height="match_parent"
    android:layout_alignParentRight="true" >

    <RelativeLayout
        android:layout_width="300px"
        android:layout_height="fill_parent"
        android:background="@android:color/holo_purple" >

        <ImageView
            android:id="@+id/iv1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/ic_action_home" />

        <ImageView
            android:id="@+id/iv2"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_below="@+id/iv1"
            android:src="@drawable/ic_action_home" />

        <ImageView
            android:id="@+id/iv3"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_below="@+id/iv2"
            android:src="@drawable/ic_action_home" />

        <ImageView
            android:id="@+id/iv4"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_below="@+id/iv3"
            android:src="@drawable/ic_action_home" />
    </RelativeLayout>
</ScrollView>

【问题讨论】:

  • 张贴一张应该是什么样子的图片。
  • 添加了所需的输出屏幕截图。
  • 使用 RecyclerView 重复滚动内容。
  • 我想使用相对布局,因为它允许我使用重叠的图像。

标签: java android scrollview android-relativelayout


【解决方案1】:

ScrollView 是一个FrameLayout,这意味着您应该在其中放置一个子,其中包含要滚动的全部内容;这个孩子本身可能是具有复杂的对象层次结构外线经理

因此,当您使用 相对布局 时,它会显示图像,但是当您使用不包含在一个布局下的所有图像的滚动视图时,它不起作用,因此请尝试将所有图像放在 下LinearLayout(推荐)或其他布局。

【讨论】:

  • 但是 relativeLayout 拥有所有图像,然后我将 relativeLayout 添加到 Scrollview ,所以理想情况下它应该显示所有带有滚动条的图像。因为当它是 Bottom_Aligned 时相同的设置正在工作。
  • 底部对齐不适用于 ScrollView 中的 RelativeLayout。 ScrollView 是无限的,它没有底。 match_parent 被 wrap_content 覆盖。
  • 从底部对齐,我的意思是完整设置(在 relativelayout 中水平添加图像并将 relativelayout 添加到滚动视图,然后将滚动视图添加到屏幕底部)。
猜你喜欢
  • 1970-01-01
  • 2011-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多