我正在尝试在Android应用中使用锁定的textField和ScrollView。我试图在运行时这样做,但我错过了一些东西。无论我首先添加到LinearLayout的对象是唯一显示的对象。我错过了什么?我没有通过xml布局文件定义任何内容。
我的主要活动是:
package com.example.scrolltest;
import com.example.scrolltest.Draw; import android.widget.ScrollView; import android.widget.LinearLayout; import android.widget.TextView; import android.os.Bundle; import android.app.Activity; import 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对象的静态示例:
package com.example.scrolltest;
import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.view.View;
公共类Draw extends View { Paint paint = new Paint();
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;
}
}
}
}
答案 0 :(得分:1)
您使用LinearLayout.LayoutParams.MATCH_PARENT
表示TextView
和ScrollView
的宽度和高度。无论您先添加哪个视图,都会填满整个LinearLayout,为其他视图留下空间。另外,我注意到您将LinearLayout的方向设置为水平。这将堆叠子视图side by side
而不是on 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()
按顺序接受参数:(宽度,高度)。您以相反的顺序提供参数。