2012-07-13 4 views
0

선형 레이아웃 (가로)으로 TextView 및 ImageButton이 있습니다. 총 너비는 300 픽셀입니다. 버튼 이미지는 50x50입니다. 텍스트에 사용할 수있는 최대 너비는 250입니다. 텍스트 너비가 250 픽셀 미만인 경우 아래 코드가 완벽하게 작동합니다 (WRAP_CONTENT는 훌륭하게 작동합니다).android layout - 오른쪽에서 왼쪽으로 WRAP_CONTENT 가능합니까?

// create relative layout for the entire view 
    LinearLayout layout = new LinearLayout(this); 
    layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
      LayoutParams.WRAP_CONTENT)); 
    layout.setOrientation(LinearLayout.HORIZONTAL); 

    // create TextView for the title 
    TextView titleView = new TextView(this); 
    titleView.setText(title); 
    layout.addView(titleView); 

    // add the button onto the view 
    bubbleBtn = new ImageButton(this); 
    bubbleBtn.setLayoutParams(new LayoutParams(
      LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT)); 
    layout.addView(bubbleBtn); 

텍스트의 길이가 250 픽셀을 넘으면 문제가 발생합니다. 버튼이 밀려 나오고 그 300 픽셀 공간에서 보이지 않게됩니다.

내가 원하는 것은 이미지에 50 픽셀 너비를 할당하는 것입니다. WRAP_CONTENT는 나머지 250 픽셀입니다. 즉, 왼쪽에서 채우는 대신 오른쪽에서 채우십시오. 중력은이 맥락에서 사용하는 옳은 일입니까? 코드에서 어떻게 그리고 어디에서 사용해야합니까?

또는 다른 방법이 더 좋으십니까?

+0

텍스트보기의 최대 폭 속성을 시도 – Som

+0

@zolio : 필요한 경우가 아니면 절대 픽셀을 사용하지 당신이 (즉 하나 개의 특정 장치를 표적으로) 당신이 무슨 일을하는지 알고 마십시오 . 'LinearLayouts'와'layout_weight'를 사용하여 안드로이드가 화면 비율을 할당하도록 허용하십시오. – Squonk

답변

1

LinearLayout 대신 RelativeLayout을 사용하십시오. 다음과 같이 각보기의의 LayoutParams을 설정합니다

// add the button onto the view 
bubbleBtn = new ImageButton(this); 
bubbleBtn.setId(1); // should set this using a ids.xml resource really. 
RelativeLayout.LayoutParams bbLP = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
bbLP.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
bbLP.addRule(RelativeLayout.CENTER_VERTICAL); 
layout.addView(bubbleBtn, bbLP); 

// create TextView for the title 
TextView titleView = new TextView(this); 
titleView.setText(title); 
titleView.setGravity(Gravity.RIGHT); 
RelativeLayout.LayoutParams tvLP = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
tvLP.addRule(RelativeLayout.LEFT_OF, 1); 
tvLP.addRule(RelativeLayout.CENTER_VERTICAL); 
layout.addView(titleView, tvLP); 
+0

프로그래밍 방식으로 ID를 설정하는 가장 좋은 방법은 다음을 참조하십시오. http://stackoverflow.com/questions/3216294/android-programatically-add-id-to-r-id – nmw

관련 문제