2013-12-17 2 views
0

그래서 버튼에 대해서만 XML 레이아웃을 만들었습니다.id가없는 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> 

main.java

public void addButton() { 
    Button btn=new Button(this); 
} 

어떻게 ID를 사용하지 않고 btn을하는 buttons.xml 레이아웃을 설정하는

?

+0

인플레이터를 사용하십시오. – njzk2

+0

을 사용했을 때 Button btn = (Button) LayoutInflater.from (this) .inflate (R.layout.buttons, null); 그래서 xml 파일에서 수학 높이와 폭을 계산합니다! –

답변

1

당신은이 작업을 수행 할 수 있습니다

<?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 > 

을 그리고 당신의 코드에서, 당신은 그 속성이 선형 레이아웃에 버튼을 추가 할 수 있습니다

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); 
0

프로그래밍 방식으로 설정할 수있는 등 HeightWidth

레이아웃 custom_button.xml

코드

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); 
} 

0

<?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> 
나는이 당신을 도울 수 있기를 바랍니다.

관련 문제