首页 文章

在运行时为Android实例化对象时,锁定文本在scrollView上方或下方的字段

提问于
浏览
0

我正在尝试在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 {Draw draw;

@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;

public class 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;
    }
    }
}

}

1 回答

  • 1

    您正在使用 LinearLayout.LayoutParams.MATCH_PARENT 作为 TextViewScrollView 的宽度和高度 . 无论您先添加哪个视图,都会填满整个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 .

    Edit:

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

相关问题