0

我正在尝试在 Android 应用程序中锁定 textField 和 ScrollView。我试图在运行时这样做,但我错过了一些东西。我首先添加到 LinearLayout 的任何对象都是唯一显示的对象。我错过了什么?我没有通过 xml 布局文件定义任何东西。

我的主要活动是:

包 com.example.scrolltest;

导入 com.example.scrolltest.Draw;导入android.widget.ScrollView;导入 android.widget.LinearLayout;导入 android.widget.TextView;导入android.os.Bundle;导入android.app.Activity;导入android.graphics.Color;

公共类 MainActivity 扩展 Activity { 绘制绘制;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
protected void onResume(){
    super.onResume();

    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.HORIZONTAL);
    int lHeight = LinearLayout.LayoutParams.MATCH_PARENT;
    int lWidth = LinearLayout.LayoutParams.MATCH_PARENT;        

    TextView tv = new TextView(this);
    tv.setText("Test text.  Boom.");

    draw = new Draw(this);
    draw.setBackgroundColor(Color.WHITE);
    ScrollView scrollView = new ScrollView(this);
    scrollView.addView(draw);

    // add the views to the layout
    ll.addView(scrollView, new LinearLayout.LayoutParams(lHeight, lWidth));

    ll.addView(tv, new LinearLayout.LayoutParams(lHeight, lWidth));

    setContentView(ll);

}

}

只是为了笑,因为 ScrollView 的内容是动态的,并且可以随时更改,我将展示我的 Draw 对象的静态示例:

包 com.example.scrolltest;

导入android.content.Context;导入android.graphics.Canvas;导入android.graphics.Color;导入android.graphics.Paint;导入android.view.View;

公共类绘制扩展视图{油漆油漆=新油漆();

public Draw(Context context) {
    super(context);            
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // Compute the height required to render the view
    // Assume Width will always be MATCH_PARENT.
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = 4000 + 50; // Since 3000 is bottom of last Rect to be drawn added and 50 for padding.
    setMeasuredDimension(width, height);
}

@Override
public void onDraw(Canvas canvas) {
    paint.setColor(Color.BLUE);

    canvas.drawLine(100, 20, 100, 1900, paint);
    paint.setColor(Color.BLACK);
       canvas.drawText("00:00", 10, 10, paint);
       int y = 0;
       int x = 200;
    for(int i = 100; i < 2900; i=i+10){

       paint.setColor(Color.GREEN);
       canvas.drawRect(x, i, x+50, i+10, paint);

    if(y == 0){
        y = 1;
        x = 200;
    } else
   {
        y = 0;
        x = 30;
    }
    }
}

}

4

1 回答 1

1

您正在使用和LinearLayout.LayoutParams.MATCH_PARENT的宽度和高度。无论您先添加哪个视图,都会填满整个 LinearLayout,而不会为另一个视图留出空间。另外,我注意到您将 LinearLayout 的方向设置为水平。这将堆叠子视图而不是. 我建议进行以下更改:TextViewScrollViewside by sideon top of each other

LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
int lHeight = LinearLayout.LayoutParams.WRAP_CONTENT;
int lWidth = LinearLayout.LayoutParams.MATCH_PARENT;        

TextView tv = new TextView(this);
tv.setText("Test text.  Boom.");

draw = new Draw(this);
draw.setBackgroundColor(Color.WHITE);
ScrollView scrollView = new ScrollView(this);
scrollView.addView(draw);

ll.addView(tv, new LinearLayout.LayoutParams(lHeight, lWidth));

// add the views to the layout
ll.addView(scrollView, new LinearLayout.LayoutParams(lHeight, lWidth));

setContentView(ll);

这将为您TextView提供顶部和ScrollView底部。

编辑:

另一件事,LayoutParams()按顺序接受参数:(宽度,高度)。您以相反的顺序提供参数。

于 2013-08-03T21:01:31.887 回答