【问题标题】:Programatically add buttons that have no visible margins以编程方式添加没有可见边距的按钮
【发布时间】:2015-02-26 12:40:00
【问题描述】:

我有一个以编程方式将按钮添加到布局的类,但我需要添加的按钮与布局底部和彼此齐平 - 没有可见的边距。

正如您在随附的屏幕截图中所见 - 按钮下方、按钮之间以及按钮的左右两侧都有边距。我需要没有边距 - 按钮应该填满布局的整个下部。

这是容器布局的 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/someclass_rootlayout"
android:orientation="vertical"
android:background="@android:color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="somepackage.someclass"
>


<LinearLayout
    android:id="@+id/someclass_buttonlayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:gravity="bottom"
    android:orientation="horizontal"
    />

</LinearLayout>

这是我的代码,用于以编程方式在一个类中添加按钮,该类接收一个数组 (buttonNames) 作为输入,其中包含要添加的按钮的字符串名称。出于测试目的,我总是传递一个包含两个元素的数组并将它们定位在布局的左侧和右侧。

private Window window;
private WindowManager.LayoutParams windowParams;

private int resourceIdentifier = 0;

...

 @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.someclass_fragment, container, false);
    ButterKnife.inject(this, view);

    window = getDialog().getWindow();

    windowParams = window.getAttributes();

    window.requestFeature(Window.FEATURE_NO_TITLE);

    someclass_buttonlayout.setWeightSum(buttonNames.size());

    for (String string : buttonNames) {
        addNewButton(string);
        resourceIdentifier++;
    }

    return view;
}

private void addNewButton(String name) {
    Button newbutton = new Button(context);
    newbutton.setText(name);
    newbutton.setId(resourceIdentifier);
    newbutton.setTextColor(getResources().getColor(R.color.black));
    newbutton.setMinHeight(0);
    newbutton.setMinWidth(0);
    newbutton.setShadowLayer(0,0,0,0);
    newbutton.setBottom(0);
    newbutton.setPadding(0,0,0,0);

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

    if (resourceIdentifier==0) {
        layoutParams.gravity = Gravity.LEFT;
    } else {
        layoutParams.gravity = Gravity.RIGHT;
    }

    layoutParams.weight = 1;

    newbutton.setLayoutParams(layoutParams);

    newbutton.setOnClickListener(this);

    newbutton.setTag(name);
    someclass_buttonlayout.addView(newbutton);

}

有没有人知道为什么尽管这些按钮周围仍然有边距,以及是否有任何方法可以消除该边距?

【问题讨论】:

  • @PsyDuck 谢谢,但实际上它们有重量。 buttonlayout 的 weightsum 等于传递给类的数组的大小(要添加的按钮数),每个按钮的权重为 1。
  • @PsyDuck 只是 android 提供的默认按钮 - 不操纵背景
  • @PsyDuck 但是,既然你提到了它。我尝试使用 setBackgroundColor 更改按钮的颜色,但边距突然消失了。它们一定是按钮上的某种 3 维化,根本不是真正的边距:/

标签: java android button layout


【解决方案1】:

您应该在动态创建按钮时为按钮添加背景颜色,如下所示:

newbutton.setBackgroundColor(Color.parseColor("#848482")); //#848482 is dark gray

【讨论】:

    【解决方案2】:

    为您的按钮设置背景颜色或可绘制。这将删除填充和边距。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-20
      • 1970-01-01
      • 2020-09-23
      • 1970-01-01
      相关资源
      最近更新 更多