简体   繁体   English

Android以编程方式设置按钮布局

[英]Android Programmatically set button layout

Hi I'm trying to load a button into a view pager and i have it loading in but it's currently filling the entire screen i would like it it to wrap content to its exact size and then position it in the middle of the screen. 嗨,我正在尝试将一个按钮加载到视图寻呼机中,并且正在加载它,但是当前正在填充整个屏幕,我希望它可以将内容包装到其确切大小,然后将其放置在屏幕中间。 i have tried to set layoutparams for the button but when i run the app it still fills parent. 我试图为按钮设置layoutparams,但是当我运行该应用程序时,它仍然会填充父级。 does anyone know how to programmatically set layout and position of a button? 有谁知道如何以编程方式设置按钮的布局和位置?

heres what i have tried so far 到目前为止我一直在尝试

@Override
        public Object instantiateItem(View collection, int position) {

            RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            rel_btn.addRule(RelativeLayout.CENTER_VERTICAL);
            rel_btn.addRule(RelativeLayout.CENTER_HORIZONTAL);
            rel_btn.height = 60;
            rel_btn.width = 60;
            Button tv = new Button(cxt);
            tv.setLayoutParams(rel_btn);
            tv.setText("League " + (position+1));
            tv.setTextColor(Color.WHITE);
            tv.setTextSize(30);
            tv.setBackgroundDrawable(getResources().getDrawable(R.drawable.ls_level_eng1));

            ((ViewPager) collection).addView(tv,0);

            return tv;
        }

Is there any particular reason you're doing this via code? 您通过代码执行此操作有任何特殊原因吗? You could simply inflate a layout and use the Layout Editor to help you adjust your Button . 您可以简单地为布局充气,并使用布局编辑器来帮助您调整Button That way you won't be guessing so much. 这样,您将不会有太多猜测。

public Object instantiateItem(View collection, int position) {

final LayoutInflater mLayoutInflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

final View view = inflater.inflate(R.layout.your_layout, null);

((ViewPager) collection).addView(view, 0);

return view;

In that case, you need to do this. 在这种情况下,您需要这样做。 I added notes within the code. 我在代码中添加了注释。

    public Object instantiateItem(View collection, int position) {

    // Create your LayoutParams
    final RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);

    // Add your rules
    rel_btn.addRule(RelativeLayout.CENTER_VERTICAL);
    rel_btn.addRule(RelativeLayout.CENTER_HORIZONTAL);

    // And anything extra
    rel_btn.height = 60;
    rel_btn.width = 60;

    // Create a new RelativeLayout for your RelativeLayout.LayoutParams
    RelativeLayout relativeLayout = new RelativeLayout(MainActivity.this);

    // Give your RelativeLayout LayoutParams
    relativeLayout.setLayoutParams(rel_btn);

    // Create your Button
    Button tv = new Button(MainActivity.this);
    tv.setText("League " + (position + 1));
    tv.setTextColor(Color.WHITE);
    tv.setTextSize(30);
    tv.setLayoutParams(rel_btn);
    // You don't need to use setBackgroundDrawable if you're only
    // grabbing a Resource
    tv.setBackgroundResource(R.drawable.ls_level_eng1);

    // Add your Button to your RelativeLayout
    relativeLayout.addView(tv);

    // Return your RelativeLayout
    ((ViewPager)collection).addView(relativeLayout, 0);
    return relativeLayout;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM