有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java滚动视图编程

我需要直接从Java代码创建一个scrollview,所以我编写了这段代码(我在AlertDialog中使用)

TextView myTextView = new TextView(context);
myTextView.setText("Very long text" + longTextVariable);
ScrollView scroll = new ScrollView(context);
scroll.setBackgroundColor(安卓.R.color.transparent);
scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
builder.setView(myTextView);
builder.setView(scroll);

但它不起作用,因为我什么也看不见。为了让你明白我在做什么,我只告诉你,我需要显示的文本非常长,没有滚动视图是“剪切”


共 (3) 个答案

  1. # 1 楼答案

    试试这个:

    ScrollView scrlView = new ScrollView(this);
    scrlView.setLayoutParams(new LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.WRAP_CONTENT));
    
    TextView txt = new TextView(this);
    txt.setText("test");
    scrlView.addView(txt);
    
    setContentView(scrlView);
    
  2. # 2 楼答案

    首先将myTextView设置为builder中的视图,然后将其替换为scroll。这样,您只有一个空的滚动视图

    您需要将myTextView添加到scrollview,然后将scrollview添加到builder

  3. # 3 楼答案

    首先需要在ScrollView中添加TextView,然后添加此ScrollView警报

    scroll.addView(myTextView);
    builder.setView(scroll);
    

    谢谢