【发布时间】: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