【问题标题】:make a single button width to fill parent programmatically以编程方式制作单个按钮宽度以填充父级
【发布时间】:2014-05-18 16:04:26
【问题描述】:

如何使单个按钮宽度以编程方式填充父级?我已经这样做了,但它似乎无法工作它仍然位于左上角,宽度只是包裹内容......这是一些关于按钮创建的代码......

public class TEST_GIFDisplay extends Activity implements View.OnClickListener {

    SurfaceView sview;
    GifRun gr = new GifRun();
    Button btnBack;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sview = new SurfaceView(this);
        sview.setZOrderOnTop(true);

        gr.LoadGiff(sview, this, R.drawable.smiley);

        LinearLayout linearLayout1 = new LinearLayout(this);
        linearLayout1.setOrientation(LinearLayout.VERTICAL);
        linearLayout1.setBackgroundColor(Color.parseColor("#27ae60"));
        linearLayout1.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));

        LinearLayout linearLayout2 = new LinearLayout(this);
        linearLayout2.setOrientation(LinearLayout.VERTICAL);
        linearLayout2.setBackgroundColor(Color.parseColor("#27ae60"));
        linearLayout2.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));

        LinearLayout.LayoutParams p = new   
        LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,   
        LinearLayout.LayoutParams.WRAP_CONTENT);
        p.weight = 1.0f;

        btnBack = new Button (this);
        btnBack.setText("Back");
        btnBack.setBackgroundColor(Color.parseColor("#f1c40f"));
        btnBack.setGravity(Gravity.CENTER_HORIZONTAL);
        btnBack.setLayoutParams(p);
        btnBack.setOnClickListener(this);

        linearLayout2.addView(btnBack);
        linearLayout1.addView(linearLayout2);
        linearLayout1.addView(sview);

        setContentView(linearLayout1);
    }

    @Override
    public void onClick(View view) {
        btnBack = (Button) view;
        btnBack.setBackgroundColor(Color.parseColor("#2980b9")); //color change
        new CountDownTimer(100, 50) {

            @Override
            public void onTick(long arg0) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onFinish() {
                btnBack.setBackgroundColor(Color.parseColor("#f1c40f")); // original color
            }
        }.start();//end color changed

        finish();
    }
}

【问题讨论】:

  • 你在哪里添加这个按钮到你的布局中,也请发布那个代码。
  • @MrSMAK 我已经用完整代码编辑了代码,我想创建一个显示 gif 图像并能够返回上一个屏幕的活动,所以按钮是转到上一个屏幕跨度>

标签: android button width parent fill


【解决方案1】:

您正在将Button 添加到linearLayout2。您应该将linearLayout2's 宽度更改为MATCH_PARENT

linearLayout2.setLayoutParams(new ViewGroup.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.WRAP_CONTENT));

我希望这会有所帮助。

P.S:您可以为按下和选定状态创建Button's 选择器,而不是使用计时器来显示按下按钮的效果。这是一个可以帮助您实现这一目标的基本链接:android button selector

【讨论】:

  • 非常感谢@MrSMAK!有效!!顺便说一句,对于您共享的 android 选择器链接,如果我的按钮是以编程方式而不是在 xml 布局中创建的,如何将按钮背景设置为 xml 选择器文件?提前感谢!
  • 我的荣幸 :-),您可以将选择器添加到您的 drawable 目录并将其设置为您的按钮,如下所示:Button b = new Button(Context); b.setBackgroundResource(R.drawable.YOUR_XML_SELECTOR);
  • 是的,再次感谢您对按钮颜色更改的回答!!!它起作用了,但不仅仅是按钮,整个屏幕都设置为相同的按钮效果。为什么会这样?对不起,我是新手!
猜你喜欢
  • 2015-01-01
  • 2019-09-20
  • 2019-12-14
  • 1970-01-01
  • 1970-01-01
  • 2018-02-16
  • 1970-01-01
  • 2021-12-18
  • 2021-01-31
相关资源
最近更新 更多