Android 是否将动态按钮布局设置为没有id的XML文件?

Android 是否将动态按钮布局设置为没有id的XML文件?,android,Android,所以我只为按钮创建了XML布局 buttons.xml <?xml version="1.0" encoding="utf-8"?> <Button xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="35dp" android:layout_height="35dp" android:layout_marginLeft="5dp" an

所以我只为按钮创建了XML布局

buttons.xml

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:background="@drawable/b1" >
</Button>
那么,如何在不使用id的情况下将buttons.xml布局设置为btn呢

您可以这样做:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:utilidades="http://schemas.android.com/apk/res/com.movidromo.ifilmfest"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:id="@+id/buttonContainer">
</LinearLayout >

您可以通过编程方式设置
高度
宽度

布局
自定义按钮.xml

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:drawablePadding="5dp"
    android:background="@android:color/transparent"
    android:gravity="center"
    android:singleLine="true">
</Button>
我希望这能帮助你。

当我使用按钮btn=(按钮)布局时,从(这个)变平。充气(R.layout.buttons,null);所以它在xml文件中包含了数学高度和宽度!
Button yourNewButton = new Button (getBaseContext());
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
lparams.setMargins(0, 0, 0, 0);
yourNewButton.setLayoutParams(lparams);
yourNewButton.setPadding(0, 0, 0, 0);
yourNewButton.setGravity(Gravity.CENTER);
yourNewButton.setBackgroundResource(R.drawable.yourBackground);
LinearLayout layButtons = (LinearLayout) findViewById(R.id.buttonContainer);
layButtons.addView(yourNewButton);
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:drawablePadding="5dp"
    android:background="@android:color/transparent"
    android:gravity="center"
    android:singleLine="true">
</Button>
private void createView() {
    btn = (Button)(context.getLayoutInflater().inflate(R.layout.custom_button, null));
    btn.setCompoundDrawablesWithIntrinsicBounds(0, iconId, 0, 0);
    btn.setText(btnText);
    btn.setTextColor(btnTextColor);
    btn.setTextSize(btnTextSize);
    btn.setBackgroundDrawable(btnBackGrad);
    btn.setMinimumHeight(50);
    btn.setWidth(50);
    btn.setPadding(0, 5, 0, 0);
}