调整放置在LinearLayout上的FancyButton之间的大小和间隙

时间:2015-05-17 13:18:38

标签: android android-layout

我已在LinearLayout上以编程方式生成FancyButtons。但是,生成的按钮放置得太紧凑,换句话说,两个连续按钮之间没有分离。此外,我希望按钮可以拉伸整个布局。我试了btnWordList.setMinimumWidth(MATCH_PARENT)而没有任何结果。请找到以下代码。

FlexDict.java

package in.dipanjan.flexdict;

import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.graphics.Color;
import android.content.Intent;
import android.widget.LinearLayout;
import android.graphics.PixelFormat;
import mehdi.sakout.fancybuttons.FancyButton;
import android.support.v7.app.ActionBarActivity;

public class FlexDict extends ActionBarActivity implements View.OnClickListener {

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        Window window = getWindow();
        window.setFormat(PixelFormat.RGBA_8888);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        int listCount, wordLists = 5;

        /* http://stackoverflow.com/questions/19078461/android-null-pointer-exception-findviewbyid */
        setContentView(R.layout.activity_flex_dict);
        LinearLayout container = (LinearLayout)findViewById(R.id.container);

        for(listCount = 1; listCount <= wordLists; listCount++)
        {
            /* https://github.com/medyo/fancybuttons */
            FancyButton btnWordList = new FancyButton(this);
            btnWordList.setId(listCount);
            btnWordList.setText("WordList " + listCount);
            btnWordList.setBackgroundColor(Color.parseColor("#3b5998"));
            btnWordList.setFocusBackgroundColor(Color.parseColor("#5474b8"));
            btnWordList.setTextSize(20);
            btnWordList.setIconResource("\uf04b");
            btnWordList.setRadius(10);
            btnWordList.setOnClickListener(this);
            container.addView(btnWordList);
        }

        setContentView(container);
    }

    @Override
    public void onClick(View view) {
        int wordList = view.getId();

        /*
        * http://www.java-samples.com/showtutorial.php?tutorialid=1525
        * http://stackoverflow.com/questions/7980627/pressing-back-button-did-not-go-back-to-previous-activity-android
        */
        Bundle params = new Bundle();
        params.putInt("WordList", wordList);
        Intent intent = new Intent(this, ShowList.class);
        intent.putExtras(params);
        startActivity(intent);
    }
}

activity_flex_dict.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    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="@drawable/radialback">
</LinearLayout>

UI

http://s26.postimg.org/rkb0r4ys9/Fancy_Button.png

3 个答案:

答案 0 :(得分:1)

您需要setLayoutParams上的View

即。 btnWordList.setLayoutParams(new LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT));

即使以XML或Programatically方式实例化,您也需要为任何视图设置布局参数。

http://developer.android.com/reference/android/view/View.html#setLayoutParams(android.view.ViewGroup.LayoutParams)

  

设置与此视图关联的布局参数。这些供应参数指向此视图的父级,指定应如何排列。 ViewGroup.LayoutParams有许多子类,它们对应于负责安排子节点的ViewGroup的不同子类。

答案 1 :(得分:1)

您需要在按钮之间设置一个边距,以便在按钮之间进行瞎扯。并将您的宽度设置为match_parent以使按钮拉伸到布局的宽度

int marginBottom = (int) TypedValue.applyDimension(
    TypedValue.COMPLEX_UNIT_DIP,
    5, 
    r.getDisplayMetrics()
 );

for(listCount = 1; listCount <= wordLists; listCount++)
{
      /* https://github.com/medyo/fancybuttons */
      FancyButton btnWordList = new FancyButton(this);
      btnWordList.setId(listCount);
      LayoutParams params = new LayoutParams(
            LayoutParams.MATCH_PARENT,      
            LayoutParams.WRAP_CONTENT
      );
      params.setMargins(0, 0, 0, marginBottom);
      btnWordList.setLayoutParams(params);
      btnWordList.setText("WordList " + listCount);
      btnWordList.setBackgroundColor(Color.parseColor("#3b5998"));
      btnWordList.setFocusBackgroundColor(Color.parseColor("#5474b8"));
      btnWordList.setTextSize(20);
      btnWordList.setIconResource("\uf04b");
      btnWordList.setRadius(10);
      btnWordList.setOnClickListener(this);
      container.addView(btnWordList);
}

答案 2 :(得分:1)

您可以使用LayoutParam保证金

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT);
lp.bottumMargin = 2;

//或 lp.setMargins(0,0,0,2);

btnWordList.setLayoutParams(lp);