2010-08-23 4 views
1

EditTextButton을 한 줄에 표시하고 싶습니다. 그러나 버튼의 크기는 일부 픽셀에 고정되어 있습니다. 내가 코드를 수행하여 계산하고android에서 Button의 너비를 얻는 방법

<LinearLayout 
    android:id="@+id/middleLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" 
    > 
<EditText 
    android:layout_height="50px" 
    android:layout_width="wrap_content" 
    android:id="@+id/searchBox" 
    android:layout_marginTop="2px" 
    android:visibility="visible" 
    android:singleLine="true" 
    android:hint="Enter Zip Code, City &amp; State" 
    /> 
<Button 
    android:layout_height="wrap_content" 
    android:layout_width="20px" 
    android:id="@+id/searchBtn" 
    android:visibility="visible" 
    android:layout_marginTop="3px" 
    android:text="Search" 
    /> 
</LinearLayout> 

을 다음과 같이 내가 수직 또는 수평 나의 XML 파일로 방향을 변경하는 방법에 EditText의 폭입니다 조정할 수 있도록 내가 프로그래밍 방식 Button의 폭을 얻고 싶었다.

searchBox = (EditText) findViewById(R.id.searchBox); 
searchbutton = (Button) findViewById(R.id.searchBtn); 
int searchIconWidth = searchbutton.getWidth(); 
searchBox.setWidth(getting_screen_width() - searchIconWidth); 

은 화면의 폭을 계산 나는

private int getting_screen_width() 
    { 
    DisplayMetrics dm = null; 
    dm = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(dm); 
    String width = ""+dm.widthPixels ; 
    dm = null; 
     return Integer.parseInt(width); 
    } 

에 따라 사용하지만 EditText보다 위의 코드를 실행하면 전체 폭 & 검색 버튼이 표시되지 않습니다 소요했다. 픽셀 단위로 단추 너비를 가져 오는 데 문제가있을 수 있습니다.

도와주세요. 미리 감사드립니다.

+0

너비가 문제가 있다고 생각합니다. 둘 다에 동일한 값을 설정하십시오. – Praveen

답변

0

아마도 풍경에 다른 레이아웃을 사용하고 싶을 것입니다. this link을 확인하십시오.

layout-land이라는 /res/ 경로에 새 폴더를 만드는 가로 방향 레이아웃의 두 번째 레이아웃을 기본적으로 가질 수 있습니다. 같은 이름으로 새 레이아웃을 배치하면 안드로이드가 나머지를 처리합니다.

+0

당신이 맞습니다,하지만 내 문제는, 내가 단추의 너비를 인쇄하려고 할 때 Logcat에 0을 보여줍니다. 그래서 EditText는 전체 화면을 사용하고 있습니다. 그 버튼에 xml로 주어진 너비가 프로그램에서 0px로 취급되는지 궁금합니다. – Ashay

+0

그 로그 라인은 어디에 배치합니까? – Macarse

+0

searchIconWidth 변수의 값을 인쇄 중입니다. 내 디버깅 부분에 여기에 게시하지 마십시오. Log.d ("ButtonWidth", ""+ searchIconWidth); 하지만이 줄 뒤에 배치 할 수 있습니다. int searchIconWidth = searchbutton.getWidth(); – Ashay

관련 문제