android 怎么在类中添加滚动条

android 怎么在类中添加滚动条,第1张

滚动条是容器, 先添加滚动条、 JList添加 到滚动条里

自定义VIEW class xxxx extends View{ }// 继承自view,然后会提示你选哪个构造方法,选择一个你要的构造,并重写父类方法

// 构造一个 RelativeLayout,设置其背景色

al = new RelativeLayout(this)

al.setBackgroundColor(Color.YELLOW)

RelativeLayout.LayoutParams tvLP = new RelativeLayout.LayoutParams(

LayoutParams.FILL_PARENT, 30)

ll = new LinearLayout(this)

ll.setId(1)

ll.setOrientation(LinearLayout.VERTICAL)

ll.setBackgroundColor(Color.GRAY)

Button button = new Button(this)

button.setText("退出")

button.setLayoutParams(LP_WW)

ll.addView(button)

al.addView(ll, tvLP)

v = new DrawPage(this)

RelativeLayout.LayoutParams vLP = new RelativeLayout.LayoutParams(

LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)

vLP.addRule(RelativeLayout.PERSISTENT_ALL_CACHES, 1)

// 也可以先为子 View 设置 LayoutParams,然后再调用一个参数的 ViewGroup.addView(View)

// 来添加。效果是一样的。

al.addView(v, vLP)

this.setContentView(al)

}

class DrawPage extends View {

private int scrollX = 0

private int scrollY = 0

Paint paint = new Paint()

Bitmap me = null, me1 = null, me2 = null, me3 = null, me4 = null,

me5 = null

Rect table1 = new Rect(0, 0, 155, 120)

Rect table2 = new Rect(155, 0, 311, 120)

Rect table3 = new Rect(0, 120, 155, 240)

Rect table4 = new Rect(0, 240, 155, 360)

Rect table5 = new Rect(0, 360, 155, 480)

Rect table6 = new Rect(0, 480, 155, 600)

Rect topChair = new Rect(53, 10, 77, 29)

public DrawPage(Context context) {

super(context)

me = loadImage(R.drawable.table4, table1)

me1 = loadImage(R.drawable.table4, table2)

me2 = loadImage(R.drawable.table4, table3)

me3 = loadImage(R.drawable.table4, table4)

me4 = loadImage(R.drawable.table4, table5)

me5 = loadImage(R.drawable.table4, table6)

}

@Override

protected void onDraw(Canvas canvas) {

// TODO Auto-generated method stub

super.onDraw(canvas)

canvas.drawBitmap(me, 0, 0, paint)

canvas.drawBitmap(me1, 0, 0, paint)

canvas.drawBitmap(me2, 0, 0, paint)

canvas.drawBitmap(me3, 0, 0, paint)

canvas.drawBitmap(me4, 0, 0, paint)

canvas.drawBitmap(me5, 0, 0, paint)

}

public Bitmap loadImage(int fileid, Rect rect) {

Resources r = this.getContext().getResources()

Bitmap bitmap = Bitmap.createBitmap(rect.right, rect.bottom,

Config.ARGB_8888)

Drawable drawable = r.getDrawable(fileid)

Canvas canvas = new Canvas(bitmap)

drawable.setBounds(rect)

drawable.draw(canvas)

return bitmap

}

}

谁说没用,亲测可以

xml布局里写:

<HorizontalScrollView

      android:layout_width="200dp"

      android:layout_height="fill_parent">

<ListView

android:layout_width="400dp"

android:layout_height="fill_parent"

android:footerDividersEnabled="false"

android:headerDividersEnabled="false"

android:scrollingCache="false"

/>

</HorizontalScrollView>

一个横向滚动且纵向滚动的listView不就出来了


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11953335.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存