2014-11-01 2 views
1

버튼과 EditText가 같은 높이 (화면의 아래쪽)에 정렬되고 화면 너비를 공유한다고 가정 해 보겠습니다. 버튼에 필요한 공간 만 남기고 editText는 나머지 너비를 덮고 싶습니다.상대 레이아웃에서 요소간에 화면의 너비를 나눕니까?

지금까지 linearLayout을 사용했습니다.이 경우 두 요소를 수평 선형 레이아웃 안에 넣을 수 있으며 editText의 무게를 1로 설정하고 버튼의 끝을 wrap_content로 설정하면 작동 할 것이다.

그러나 상대 레이아웃을 사용하여이 동작을 복제 할 수 없습니다. 부모의 맨 아래에 editText를 놓고 버튼을 오른쪽으로 설정할 수 있지만 editText가 버튼의 공간을 존중하는 나머지 너비를 채우는 방법을 모르겠습니다.

올바른 방법은 무엇입니까?

편집 :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 



    <EditText 
     android:id="@+id/edittext" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:hint="@string/editText_Hint" 
     android:inputType="number" /> 


    <Button 
     android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignRight="@id/edittext" 
     android:onClick="sendAnswer" 
     android:text="@string/button_text" /> 

</RelativeLayout> 

그리고 나는이의 행동을 복제 할 :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    /*other stuff here*/ 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 


     <EditText 
      android:id="@+id/edittext" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:hint="@string/editText_Hint" 
      android:inputType="number" 
      android:layout_weight="1" 
      /> 


     <Button 
      android:id="@+id/button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/button_text" 
      android:onClick="sendAnswer"/> 
    </LinearLayout> 
</LinearLayout> 
+0

귀하의 XML 코드 –

+0

@Dilavar 편집을 넣어주세요 코드가 포함되었습니다. – kace91

+0

첫 번째 (XML에서) 너비로 내용을 부모의 아래쪽 및 오른쪽으로 줄입니다 (정렬). 다음 편집 왼쪽 하단 부모 (정렬) 및 단추 너비의 오른쪽 0dp – Selvin

답변

1

버튼과 설정 버튼을 오른쪽으로 정렬이 시도를이 내가 지금까지 무엇을 가지고 의 edittext ~ match_parent이 정상적으로 작동합니다.

<RelativeLayout 
    android:id="@+id/relativeLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/button1" 
     android:ems="10" > 

     <requestFocus /> 
    </EditText> 
</RelativeLayout> 

이렇게하면 edittext은 경계까지가는 것처럼 보입니다. 속성을 사용하여 edittext의 오른쪽에 약간의 여백을 추가 더 잘하게하려면 android:layout_marginRight="5dp"

UPDATE

귀하의 요구 사항은 Relative Layout, 중첩을 사용하기에 가장 적합한 솔루션으로 부모 레이아웃을 사용하는 경우 형세.

선형 레이아웃 내에서 android:layout_alignParentBottom="true"으로 중첩 된 LinearLayout을 LinearLayout에 사용하십시오.

샘플 아래,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" > 

     <EditText 
      android:id="@+id/editText1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:ems="10" > 

      <requestFocus /> 
     </EditText> 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button" /> 

    </LinearLayout> 

</RelativeLayout> 
+0

이것은 지금까지 가지고있는 최고의 솔루션이지만 여전히 editText가 버튼과 겹치는 너비를 채우고있는 것처럼 보입니다.나는 레이아웃을 바꿀 수있다 : 버튼의 너비와 일치 할 때까지 시행 착오에 의해 수동으로 여백을 맞출 수 있지만 더 나은 방법이 있어야한다. (버튼의 너비가 다른 언어로 바뀌면 아마 하드 코드 된 숫자가 작동하지 않을 것이다) – kace91

+1

@ kace91 내 업데이트를 살펴보십시오. 상대 레이아웃에서 중첩 된 선형 레이아웃을 사용하는 것이 좋으면 도움이 될 것입니다. 이 구현은 하드 코딩 된 값을 사용하지 않습니다. – codePG

+1

그래, 그게 효과가 있다는 것을 알았지 만, 성능에 영향을 미친다는 의미에서 레이아웃을 중첩시키지 않으려 고 노력했습니다. 어쨌든 중첩없이 작동하는 솔루션을 찾았습니다. 확인하고 싶을 경우 별도의 응답으로 게시했습니다. 어쨌든 도움을 주셔서 감사합니다! – kace91

0

가 나는 해결책을 찾아 냈다. 트릭은 두 요소를 아래쪽에 설정 한 다음 텍스트를 너비로 편집하는 것입니다. 0dp, alignParentLeft 및 toLeftOF 버튼 (버튼의 alignLeft가 아니라 buttonLeftOF). 버튼은 부모 오른쪽과 부모 아래쪽에 정렬되어야합니다.

마지막 코드는 다음과 같습니다

<EditText 
     android:id="@+id/edittext" 

     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_toLeftOf="@id/button" 
     android:layout_alignParentLeft="true" 
     android:hint="@string/editText_Hint" 
     android:inputType="number" 
     android:layout_width="0dp" 
     > 

    <Button 
     android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:onClick="sendAnswer" 
     android:text="@string/button_text" /> 
관련 문제