Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 以编程方式使按钮居中_Android_Android Layout_Android Activity - Fatal编程技术网

Android 以编程方式使按钮居中

Android 以编程方式使按钮居中,android,android-layout,android-activity,Android,Android Layout,Android Activity,我试图以编程方式创建一个居中按钮,按下该按钮时可以打印一个单词。 我使用了重力,但是我得到了两个错误: eglSurfaceAttrib not implemented Failed to set EGL_SWAP_BEHAVIOR on surface 0xae0ee360, error=EGL_SUCCESS 下面是我的代码: import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import

我试图以编程方式创建一个居中按钮,按下该按钮时可以打印一个单词。 我使用了重力,但是我得到了两个错误:

eglSurfaceAttrib not implemented
Failed to set EGL_SWAP_BEHAVIOR on surface 0xae0ee360, error=EGL_SUCCESS
下面是我的代码:

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;


public class MainActivity extends ActionBarActivity {

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

/////////////

@Override
protected void onCreate(Bundle bundle) {

    super.onCreate(bundle);
    setContentView(R.layout.activity_main);

    final ViewGroup vg1 = (ViewGroup) findViewById(android.R.id.content);
    float w1 = vg1.getWidth();
    float h1 = vg1.getHeight();

    final Button b1 = new Button(this);

    b1.setText("");

    float left = (w1 - vg1.getWidth())/2;
    float top = (h1 - vg1.getHeight())/2;

    FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams((int)w1/2, (int)h1/2);
    layoutParams.setMargins( (int)left,  (int)top, (int)(left+w1/2), (int)(top+h1/2));
    layoutParams.gravity = Gravity.CENTER;

    b1.setLayoutParams(layoutParams);
    vg1.addView(b1, layoutParams);

    b1.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {                
                b1.setText("TestTest");
        }
    });
}
以及xml:

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

请帮我找出我的错误。 我刚开始学习android开发,在这方面没有真正的经验


谢谢大家!

为了获得布局的高度和宽度,您需要等待measure()过程(此处有更多信息)。 您可以在布局上添加侦听器,例如:

gv1.getViewTreeObserver().addOnGlobalLayoutListener(
    new ViewTreeObserver.OnGlobalLayoutListener() {
        public void onGlobalLayout() {
            // execute your code here, after height and width have been calculated
        }
    }
);

float w1=vg1.getWidth()
float h1=vg1.getHeight()可能是零。是的,你是对的。但是,这会导致错误吗?这是一个OpenGL错误,通常会在模拟器上弹出。当涉及到布局时,通常不需要担心。