在Android运行时实例化对象时,锁定scrollView上方或下方的文本字段
我试图在Android应用程序中有一个锁定的文本字段和一个滚动视图。我试图在运行时这样做,但我遗漏了一些东西。我首先添加到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扩展了活动{ 抽签在Android运行时实例化对象时,锁定scrollView上方或下方的文本字段,android,scrollview,android-linearlayout,textview,Android,Scrollview,Android Linearlayout,Textview,我试图在Android应用程序中有一个锁定的文本字段和一个滚动视图。我试图在运行时这样做,但我遗漏了一些东西。我首先添加到LinearLayout的任何对象都是唯一显示的对象。我错过了什么?我没有通过xml布局文件定义任何内容 我的主要活动是: 包com.example.scrolltest 导入com.example.scrolltest.Draw; 导入android.widget.ScrollView; 导入android.widget.LinearLayout; 导入android.wi
@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);
}
}
对于grins,由于ScrollView的内容是动态的,并且可以随时更改,我将显示一个Draw对象的静态示例:
包com.example.scrolltest
导入android.content.Context;
导入android.graphics.Canvas;
导入android.graphics.Color;
导入android.graphics.Paint;
导入android.view.view
公共类绘制扩展视图{
油漆=新油漆()
公共绘图(上下文){
超级(上下文);
}
@凌驾
测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
//计算渲染视图所需的高度
//假设宽度始终与父项匹配。
int width=MeasureSpec.getSize(widthmasurespec);
int height=4000+50;//因为3000是要添加的最后一个矩形的底部,50用于填充。
设置测量尺寸(宽度、高度);
}
@凌驾
公共空白onDraw(画布){
油漆。设置颜色(颜色。蓝色);
帆布.抽绳(100,20,100,1900,油漆);
油漆。设置颜色(颜色。黑色);
画布。绘图文本(“00:00”,10,10,绘画);
int y=0;
int x=200;
对于(int i=100;i<2900;i=i+10){
油漆。设置颜色(颜色。绿色);
画布.drawRect(x,i,x+50,i+10,绘制);
如果(y==0){
y=1;
x=200;
}否则
{
y=0;
x=30;
}
}
}
}您正在使用
LinearLayout.LayoutParams.MATCH\u PARENT
作为TextView
和ScrollView
的宽度和高度。无论您先添加哪个视图,都将填充整个线性布局,不会为其他视图留下任何空间。另外,我注意到您将LinearLayout的方向设置为水平。这将使子视图并排堆叠
,而不是彼此重叠
。我建议作出以下修改:
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);
这将为您提供顶部的文本视图
,以及下方的滚动视图
编辑:
另一件事,
LayoutParams()
按顺序接受参数:(宽度、高度)。您正在以相反的顺序提供参数。您能解释一下为什么将ScrollView添加到线性布局中,而不是添加到反向布局中,这样仍然无法同时呈现两个视图。我在这个例子中也只看到文本视图。@WildBill请查看上面的编辑。完成了,谢谢!我确信这与布局参数有关,但由于这对我来说是全新的,我不确定从哪里开始。当你对某件事有深深的怀疑时,调查它可能会让人望而生畏。非常感谢@WildBill‘当你对某件事有深深的怀疑时,调查它可能会让人望而生畏’:我知道你的意思。很高兴我能帮忙。
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);