UFO ET IT

다른 요소의 높이와 일치하도록 버튼의 높이를 얻는 방법은 무엇입니까?

ufoet 2021. 1. 12. 08:09
반응형

다른 요소의 높이와 일치하도록 버튼의 높이를 얻는 방법은 무엇입니까?


EditText 옆에 버튼을 놓고 높이를 일치시키고 싶습니다.

예를 들어 내장 Android 브라우저에서 :

대체 텍스트

이동 버튼은 EditText 필드와 높이가 같습니다. 이 두 뷰를 부모 레이아웃 뷰로 래핑하고 두 높이를 fill_parent로 설정하면 일치하게됩니다. 그러나 레이아웃에 정적 크기를 지정하지 않고이 작업을 수행하고 싶습니다. 오히려 EditText가 글꼴 크기에 따라 필요한 높이를 취하고 그 옆에 버튼이 어떤 높이와 일치하도록 할 것입니다.

xml 레이아웃으로 가능합니까?


당신은 EditText wrap_content그것의 높이 를 만들고 Button단지 fill_parent. 둘 다 Layout부모로 감싸 야합니다 . 이렇게하면 동일한 Layout부모 와 연결됩니다 .

그것을 시도하고 그것이 어떻게 작동하는지 확인하십시오. 그렇지 않은 경우 도움이 될 코드를 줄 수 있습니다.


아마도 EditText과는 Button돌며 있습니다 RelativeLayout. 버튼의 레이아웃 속성을 alignTopalignBottomEditText.


원하는 것은 상대적인 레이아웃입니다. 몇 가지 의견이있는 예는 다음과 같습니다.

RelativeLayout 을 부모로 시작 합니다. 모든 콘텐츠를 래핑 할 수 있습니다. 그 부모에는 두 가지 요소, 버튼과 예제의 editText가 있습니다.

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

Button 요소를 오른쪽 상단에 배치하는 것으로 시작합니다. 이것이 layout_alignParentRightlayout_alignParentTop 이 모두에 관한 것입니다. 다시 이것은 가장 큰 요소이므로 높이와 너비 모두에 대해 wrap_content사용하여 모든 콘텐츠를 래핑합니다 .

<Button
    android:id="@+id/Button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="some_text" />

이제 두 번째 요소 인 editText를 이전 요소의 왼쪽에 정렬하려는 경우 layout_toLeftOf 매개 변수 와 함께 id 참조를 사용하여 이를 수행합니다.

<EditText
    android:id="@+id/EditText1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/Button1"
    android:hint="some_hint"
    android:inputType="textCapWords" />

RelativeLayout을 닫고 이제 이것을 렌더링하여 아마도 이미 얻은 것을 확인하십시오.

</RelativeLayout>

여기에 이미지 설명 입력

editText는 높이가 더 작기 때문에 옆에 배치 된 Button과 일치하지 않습니다. 이에 대한 해결책은 레이아웃 매개 변수를 더 추가하는 것입니다. 당신이 찾고있는 마법의 것은 layout_alignBottomlayout_alignParentTop 입니다.

   android:layout_alignBottom="@+id/Button1"
    android:layout_alignParentTop="true"

이 2 개를 추가하면 레이아웃이 올바르게됩니다.

여기에 이미지 설명 입력

참조 URL : https://stackoverflow.com/questions/4188859/how-to-get-a-buttons-height-to-match-another-elements-height

반응형