充气按钮的宽度和高度错误

Inflated button has wrong width and height

我正在构建导航片段。有一个始终存在的按钮(主按钮)和动态添加和删除的其他按钮。

我有一个 xml 资源文件,我从中扩展了一个按钮,更改它的图标并将其添加到片段根布局中。

几乎一切正常,除了按钮大小。之后的按钮大小为 0 (getWidth()/getHeight())。

button_navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/navigation_button"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:background="@drawable/icon_test"
    android:backgroundTint="@drawable/selector_bar_button"></Button>

充气码

Button butt = (Button) inflater.inflate(R.layout.button_navigation, null, false);

buttons.add(butt);
root_view.addView(butt);

创建充气器

public void setParent(MainActivity activity){
        parent = activity;
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

变量

MainActivity parent;
LayoutInflater inflater;

int index;

ArrayList<Button> buttons;
Button button_main;
LinearLayout root_view;

主按钮,具有与 button_navigation.xml 相同的代码,出现在片段的布局文件中。主按钮大小合适

尝试在您的 java 代码中使用它 button.setLayoutParams (new LayoutParams(60,60);

将按钮包裹在布局中。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<Button
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/nickname_message"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:text="adawd awdaw"
    android:textSize="18sp"
    android:textStyle="bold" />

然后在运行时添加它。

linearLayout.addView(LayoutInflater.from(this).inflate(R.layout.item_layout,null));