如何以编程方式向LinearLayout添加两个按钮?

时间:2014-06-18 08:35:51

标签: java android android-linearlayout

我不能addView()两次。 事实上,我想要一个TextView(左)和一个Button(右)。 那么我如何在LinearLayout中定位它们?

if (success == 1) {
    content = json.getJSONArray(TAG_COMMENTS);
    layoutVertical.removeAllViews();
    final int N = content.length();
    final Button[] myButton = new Button[N];
    final LinearLayout[] myLayout = new LinearLayout[N];
    final TextView[] myTextView = new TextView[N];
    for (int i = 0; i < content.length(); i++) {
        JSONObject c = content.getJSONObject(i);

        String name = c.getString(TAG_CONTENT);
        final LinearLayout layoutHorizontal = new LinearLayout(
                getActivity());
        layoutHorizontal
                .setOrientation(LinearLayout.HORIZONTAL);
        layoutHorizontal.setId(i);
        layoutHorizontal.setBackground(getResources()
                .getDrawable(R.drawable.borderadmincomment));
        final TextView rowTextView = (TextView) new TextView(
                getActivity());
        rowTextView.setText(name);
        rowTextView.setId(i);

        final Button rowButton = new Button(getActivity());
        rowButton.setLayoutParams(new LayoutParams(1,
                LayoutParams.MATCH_PARENT));
        rowButton.setId(i);
        rowButton.setBackgroundResource(R.drawable.croix);
        rowButton
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
        layoutHorizontal.addView(rowTextView, 0);
        layoutHorizontal.addView(rowButton, 1);
        rowTextView.setWidth(400);
        //rowTextView.setHeight(150);
        //rowButton.setHeight(150);
        layoutVertical.addView(layoutHorizontal);
        myButton[i] = rowButton;
        myLayout[i] = layoutHorizontal;
        myTextView[i] = rowTextView;
    }
}

它不起作用。

谢谢。

3 个答案:

答案 0 :(得分:0)

您忘了为水平布局和rowTextView设置布局参数。

例如:

layoutHorizontal.setLayoutParams(new
       LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

答案 1 :(得分:0)

创建线性布局参数。做这样的事情: -

LinearLayout LL = new LinearLayout(this);
LL.setBackgroundColor(Color.CYAN);
LL.setOrientation(LinearLayout.HORIZONTAL);

LayoutParams LLParams = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);

LL.setWeightSum(2f);
LL.setLayoutParams(LLParams);

请让我知道这会有效。

答案 2 :(得分:0)

创建一个xml布局文件并将其命名为item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center_vertical" >

    <TextView
        android:id="@+id/rowTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_weight="1" />

    <Button
        android:id="@+id/rowButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

在您的活动中编写以下代码

LayoutInflater inflater = (LayoutInflater)   getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View layoutHorizontal =  inflater.inflate(R.layout.item, null);
            TextView rowTextView = (TextView) layoutHorizontal.findViewById(R.id.rowTextView);
            Button rowButton = (Button) layoutHorizontal.findViewById(R.id.rowButton);
     layoutVertical.addView(layoutHorizontal);