2015-01-02 4 views
0

내 활동에 프로그래밍 방식으로 여러 개의 단추를 추가하고 싶습니다. 지금까지 잘 작동하지만 내 버튼이 너비로 압축 된 것처럼 보입니다. 버튼의 배경 이미지는 20 * 20 픽셀이며 너비와 높이에 WRAP_CONTENT를 사용합니다.androidbutton이 너비가 압축 된 것처럼 보이는 이유는 무엇입니까?

for(int i = 0; i < 5; i++){ 

     LinearLayout layout; 
     Button buttonDel; 

     layout = (LinearLayout)findViewById(R.id.linLayout); 

     LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
     layout.setOrientation(LinearLayout.VERTICAL); 

     buttonDel = new Button(this); 
     buttonDel.setId(i); 
     buttonDel.setBackgroundResource(R.drawable.buttondelete); 
     buttonDel.setLayoutParams(params); 

     layout.addView(buttonDel); 

    } 

하고 XML :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context="com.developer.cardz.ListOfAllWordsActivity"> 

<ScrollView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:id="@+id/scrollView"> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:id="@+id/linLayout"> 

    </LinearLayout> 
</ScrollView> 

당신이 어떤 제안이 있습니까 여기 코드는?

+0

내부에 텍스트가 있습니까? 버튼에 텍스트가 있으면 텍스트와 관련된 이미지를 압축 할 수있는 "내용"으로 간주됩니다. – Muthu

+0

출력물의 스크린 샷을 게시 할 수 있습니까? – droid

+0

aaah right, 텍스트가 문제입니다. 나는 ImageButton을 사용해야한다 ... 고마워! – nic2307

답변

0

텍스트가 내부에 있습니까? 버튼에 텍스트가 있으면 텍스트와 관련된 이미지를 압축 할 수있는 "내용"으로 간주됩니다.

0

몇 가지 제안을하겠습니다. 당신의 XML에 당신이 그것을 가지고 있기 때문에

LinearLayout layout; 
    Button buttonDel; 

    layout = (LinearLayout)findViewById(R.id.linLayout); 


    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 

    for(int i = 0; i < 5; i++){ 
     buttonDel = new Button(this); 
     buttonDel.setId(i); 
     buttonDel.setBackgroundResource(R.drawable.buttondelete); 
     buttonDel.setLayoutParams(params); 

     layout.addView(buttonDel); 
    } 

그리고 layout.setOrientation(LinearLayout.VERTICAL);가 필요하지 않습니다 :

코드는이 방법이 더 효율적입니다.

R.drawable.buttondelete를 표시 할 수 있습니까? 그것이 png9라면, 그것은 정상적인 것입니다. 단추에 텍스트를 넣어보십시오.

+0

감사합니다. – nic2307

+0

upvote와 함께 고마워요 :) 다행히 도왔습니다! – Olaia

+1

죄송합니다. upvote하지 않습니다. 나는 더 많은 평판이 필요하다고 말한다. – nic2307

0

Button을 ImageButton으로 변경했습니다. 그것은 그 문제를 해결했습니다. 댓글을 달았습니다 모두에게 감사드립니다!

0

그냥 당신이, 버튼를 계속 변경하려는 경우 경우 (100, 100)가 원하는 버튼 높이입니다

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100, 100);

.