如何在Android中从右侧动态添加View?

发布于 2024-12-10 15:22:07 字数 1077 浏览 0 评论 0原文

我需要在 Android 的 Fragment 中从右侧布局视图,但是,android 并没有按照我的想法布局子视图。 我尝试将TableLayout和ImageView添加到LINEARLAYOUT,ImageView的宽度是固定的,TableLayout的宽度是动态的。此外,ImageView 需要位于右侧。 部分源代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    Context c = getActivity().getApplicationContext();

    LinearLayout l = new LinearLayout(c);
    LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT, 0);
    params.gravity = Gravity.RIGHT;
    l.setLayoutParams(params);
    // l.setGravity(Gravity.RIGHT);



    PankouAttachmentView pav = new PankouAttachmentView(c, null);
    pav.setLayoutParams(params);
    l.addView(pav);

    ImageView iv = new ImageView(c);
    iv.setClickable(true);
    iv.setFocusable(true);
    iv.setImageResource(R.drawable.testarrow);
    iv.setMaxWidth(BUTTON_WIDTH);
    iv.setLayoutParams(new LayoutParams(BUTTON_WIDTH,
            LayoutParams.MATCH_PARENT));

    l.addView(iv);

    return l;
}

任何帮助将不胜感激。THX:)

I need to layout the views from the RIGHT side in a Fragment in Android, however, android did not layout the sub views as what I thought.
I tried to add a TableLayout and an ImageView to a LINEARLAYOUT, the width of the ImageView was fixed and the width of TableLayout is dynamic. Furthermore, the ImageView need to be located on the right side.
Part of the source code:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    Context c = getActivity().getApplicationContext();

    LinearLayout l = new LinearLayout(c);
    LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT, 0);
    params.gravity = Gravity.RIGHT;
    l.setLayoutParams(params);
    // l.setGravity(Gravity.RIGHT);



    PankouAttachmentView pav = new PankouAttachmentView(c, null);
    pav.setLayoutParams(params);
    l.addView(pav);

    ImageView iv = new ImageView(c);
    iv.setClickable(true);
    iv.setFocusable(true);
    iv.setImageResource(R.drawable.testarrow);
    iv.setMaxWidth(BUTTON_WIDTH);
    iv.setLayoutParams(new LayoutParams(BUTTON_WIDTH,
            LayoutParams.MATCH_PARENT));

    l.addView(iv);

    return l;
}

Any help will be appreciated.THX :)

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

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

发布评论

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

评论(1

拥有 2024-12-17 15:22:07

我不确定我完全理解你的问题,但你尝试过使用相对布局吗?相对布局比线性布局更容易完成。请参阅 android hello 视图 教程。

I'm not sure I completely understand your question but have you tried using a relative layout? Relative layout lets you accomplish much easier than a linear layout. See the android hello views tutorial.

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