Android动态添加horizo​​ntalscrollbar和verticalscrollbar到视图

发布于 2024-11-06 07:45:16 字数 410 浏览 5 评论 0原文

您好,
我在向视图动态添加水平和垂直滚动条时遇到问题。基本上该应用程序放大和缩小图像。我想每当视图超出范围时动态放置水平和垂直滚动条。即当单击放大并且视图将脱离绑定时,它会自动添加滚动条。我正在使用这两种方法,但它们不能正常工作。
       setVerticalScrollBarEnabled(true);
       setHorizo​​ntalScrollBarEnabled(true);
我是否必须将这些方法放在视图的 onDraw(Canvas canvas) 方法中?
注意:我没有使用 xml 文件创建视图。我正在使用从 View 继承的类创建视图。
谢谢你!!

HI,
I have a problem in dynamically adding horizontal and vertical scrollbar to a view . Basically the application zoom in and zoom out an image. I want to put horizontal and vertical scrollbar dynamically whenever view is out bounds. i.e. when zoom in is click and view is gonna out of bond, then it automatically add scrollbar. I am using these two methods but they didn't work correctly.
       
setVerticalScrollBarEnabled(true);
       
setHorizontalScrollBarEnabled(true);

Does i have to put these methods inside onDraw(Canvas canvas) method of the view?

Note: I am not creating view using xml file. I am creating view using a class which is inherited from View.
Thank you!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奢华的一滴泪 2024-11-13 07:45:16

不完全相同,但这可能会给你一个线索:

    mTextView = new TextView(this);
    mTextView.setGravity(Gravity.CENTER_VERTICAL);       
    mTextView.setText(R.string.instructions);
    mTextView.setTextColor(0xFF000000);
    mTextView.setPadding(20, 8, 8, 20);
    //mTextView.setBackgroundColor(0xCCFFFFFF);
    mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PT, 8);

    mScroll = new ScrollView(this);
    mScroll.setScrollbarFadingEnabled(false);
    mTextPane = new RelativeLayout(this);
    mTextPane.setVisibility(View.GONE);
    //mScroll.setVisibility(View.GONE);

    mScroll.addView(mTextView);
    mTextPane.addView(mScroll);

    Resources res = getResources();
    Drawable drawable = res.getDrawable(R.drawable.text_pane_feather2);
    mTextPane.setBackgroundDrawable(drawable);

    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( 420, 420 );
    //mBackground.setImageBitmap(R.drawable.text_pane);
    lp.setMargins(0,0,0,30);
    lp.addRule(RelativeLayout.CENTER_HORIZONTAL );
    lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

    layout.addView(mTextPane, lp);

not quite the same thing but this may give you a clue:

    mTextView = new TextView(this);
    mTextView.setGravity(Gravity.CENTER_VERTICAL);       
    mTextView.setText(R.string.instructions);
    mTextView.setTextColor(0xFF000000);
    mTextView.setPadding(20, 8, 8, 20);
    //mTextView.setBackgroundColor(0xCCFFFFFF);
    mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PT, 8);

    mScroll = new ScrollView(this);
    mScroll.setScrollbarFadingEnabled(false);
    mTextPane = new RelativeLayout(this);
    mTextPane.setVisibility(View.GONE);
    //mScroll.setVisibility(View.GONE);

    mScroll.addView(mTextView);
    mTextPane.addView(mScroll);

    Resources res = getResources();
    Drawable drawable = res.getDrawable(R.drawable.text_pane_feather2);
    mTextPane.setBackgroundDrawable(drawable);

    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( 420, 420 );
    //mBackground.setImageBitmap(R.drawable.text_pane);
    lp.setMargins(0,0,0,30);
    lp.addRule(RelativeLayout.CENTER_HORIZONTAL );
    lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

    layout.addView(mTextPane, lp);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文