中心按钮以编程方式和动态布局

时间:2014-07-07 08:37:01

标签: android button layout center

我尝试将按钮设置在布局的中心但不起作用,我需要将项目居中而不是文本。代码是:

ScrollView sv = new ScrollView(this);
sv.setBackgroundColor(Color.parseColor("#E7E7E7"));
linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setBackgroundColor(Color.parseColor("#E7E7E7"));
sv.addView(linearLayout);
for (int i = 0; i < jsonHeader.length(); i++) { setting textview and edittext to the layout}

Button datos_OK = new Button(DataObjectActivity.this);
    datos_OK.setText("Aceptar");
    RelativeLayout.LayoutParams testLP = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    testLP.addRule(RelativeLayout.CENTER_IN_PARENT);

    datos_OK.setLayoutParams(testLP);
    datos_OK.setBackgroundResource(R.drawable.button_drawable_init);
    datos_OK.setTextColor(Color.WHITE);
    linearLayout.addView(datos_OK);

该代码在我的手机上不起作用,我不知道为什么。另一个问题,我如何为我的代码设置颜色? Thanx很多。

3 个答案:

答案 0 :(得分:1)

用这个

替换你的代码
    // find main layout from xml
    linearLayout1 = (LinearLayout) findViewById(R.id.llmain);
    linearLayout1.setBackgroundColor(Color.parseColor("#E7E7E7"));

    // Add Scrollview and linearlayout
    ScrollView sv = new ScrollView(this);
    sv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT));
    linearLayout = new LinearLayout(this);
    linearLayout.setOrientation(LinearLayout.VERTICAL);

    linearLayout.setLayoutParams(new LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

    sv.addView(linearLayout);
    linearLayout1.addView(sv);

    for (int i = 0; i < 10; i++) {
        // setting textview and edittext to the layout
    }

    // Genrate button dynamically
    Button datos_OK = new Button(this);
    datos_OK.setText("Aceptar");
    datos_OK.setTextColor(Color.WHITE);
    LinearLayout.LayoutParams testLP = new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);

    testLP.gravity = Gravity.CENTER_HORIZONTAL;
    datos_OK.setLayoutParams(testLP);
    linearLayout.addView(datos_OK);

并且您的xml应如下所示

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/llmain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@android:color/black"
tools:context=".MainActivity" >

然后你会得到如下所示的输出

enter image description here

答案 1 :(得分:0)

你做错了

ScrollView sv = new ScrollView(this);
linearLayout = new LinearLayout(this);

您应该使用XML中定义的ID:

ScrollView sv = (ScrollView) findViewById(R.id.myScrollView);
linearLayout = (LinearLayout) findViewById(R.id.myLinearLayout);

并在XML

<ScrollView android:id="@+id/myScrollView"
    ..... >
<LinearLayout android:id="@+id/myLinearLayout"
    .....>

答案 2 :(得分:0)

您好请查看以下代码..

    ScrollView sv = new ScrollView(this);
    sv.setBackgroundColor(Color.parseColor("#394343"));
    LinearLayout linearLayout = new LinearLayout(this);
    linearLayout.setLayoutParams(new LinearLayout.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    linearLayout.setGravity(Gravity.CENTER);
    linearLayout.setOrientation(LinearLayout.VERTICAL);
    linearLayout.setBackgroundColor(Color.parseColor("#E7E7E7"));
    sv.addView(linearLayout);

    Button datos_OK = new Button(this);
    datos_OK.setText("Aceptar");
    LinearLayout.LayoutParams testLP = new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    testLP.setLayoutDirection(Gravity.CENTER);

    datos_OK.setLayoutParams(testLP);
    datos_OK.setBackgroundResource(R.drawable.ic_delete);
    datos_OK.setTextColor(Color.WHITE);
    linearLayout.addView(datos_OK);

让我知道您的反馈意见..