在android中动态添加按钮的问题

时间:2018-03-05 04:13:39

标签: android android-layout button

动态添加按钮的问题,如何让按钮自动转到下一行

像这样:

我想要的输出:

The Output that I wanna

我得到的输出:

The Output I got

这是我的代码:

LinearLayout ll = (LinearLayout)view.findViewById(R.id.layout);

Button btn = new Button(getContext());
btn.setText("Button1");
btn.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

Button btn1 = new Button(getContext());
btn1.setText("Button2");
btn1.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

Button btn2 = new Button(getContext());
btn2.setText("Button3");
btn2.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

Button btn3 = new Button(getContext());
btn3.setText("Button4");
btn3.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

Button btn4 = new Button(getContext());
btn4.setText("Button5");
btn4.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

ll.addView(btn);
ll.addView(btn1);
ll.addView(btn2);
ll.addView(btn3);
ll.addView(btn4);

在XML中:

 <LinearLayout
        android:orientation="horizontal"
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></LinearLayout>

我是Android新手,感谢Guys:)

3 个答案:

答案 0 :(得分:1)

使用我新设计的PredicateLayout来自动将按钮移动到下一行,如

<强> XML

 function create_event_post( $post_id ) {

    // If this is just a revision, don't create post.
    if ( wp_is_post_revision( $post_id ) )
        return;

    $post_title = get_the_title( $post_id );
    $post_type = get_post_type($post_id);
    $post_url = get_permalink( $post_id );
    $post_content = get_post_field('post_content', $post->ID);
    $featured_image_url = get_the_post_thumbnail_url($post_id, 'full');
    $author_id = get_post_field ('post_author', $post_id);
    $author_name = get_the_author_meta( 'display_name' , $author_id );  
    $slug = 'event';


// If the post is not "tribe_events", don't create a new post.  
    if ( "tribe_events" != $post_type ) 
        return;

    $post_id = post_exists( $my_title );
    if (!$post_id) {
        $post_id = wp_insert_post(
        array(
            'comment_status'    =>  'closed',
            'ping_status'       =>  'closed',
            'post_author'       =>  $author_id,
            'post_title'        =>  $post_title,
            'post_content'      =>  $post_content,
            'post_status'       =>  'publish',
            'post_type'     =>  'post'
        )
    );

}
add_action( 'save_post', 'create_event_post' );

<强>爪哇

<PredicateLayout
        android:orientation="horizontal"
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

答案 1 :(得分:0)

请将LinearLayout Orientation设置为vertical

 <LinearLayout
        android:orientation="vertical"
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></LinearLayout>

答案 2 :(得分:0)

LinearLayout linearLayoutSubParent = new LinearLayout(AddNewContactActivity.this);
        linearLayoutSubParent.setOrientation(LinearLayout.HORIZONTAL);
        linearLayoutSubParent.setWeightSum(100f); 

        LinearLayout.LayoutParams linearLayoutSubParentParams =
                new LinearLayout.LayoutParams(0,
                        LinearLayout.LayoutParams.WRAP_CONTENT, 90f); // you  can add multiple view, by providing weight.
        linearLayoutSubParent.setLayoutParams(linearLayoutSubParentParams);
        linearLayoutSubParent.setPadding(0, 0, 0, 0);

        final Button button = new Button(this);
        LinearLayout.LayoutParams editTextParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        editTextParams.setMargins(0, 10, 0, 0);
        button.setLayoutParams(editTextParams);

        button.setTextSize(TypedValue.COMPLEX_UNIT_PX,
                getResources().getDimension(R.dimen.list_item_Big_text_size));
        button.setText("Text");
        button.setInputType(InputType.TYPE_TEXT_FLAG_CAP_WORDS);
        button.setEnabled(false);
        linearLayoutSubParent.addView(button);
        linearLayoutParent.addView(linearLayoutSubParent);


 <LinearLayout
                android:id="@+id/linearLayoutParent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
              >
</LinearLayout>