RelativeLayout에서 버튼을 중앙에 배치 할 수 있습니까?
상대 레이아웃에서 버튼을 중앙에 배치하려고하는데 이것이 가능합니까? 나는 중력 및 방향 기능을 시도했지만 아무것도하지 않습니다.
시험
android:layout_centerHorizontal="true"
정확히 이와 같이 작동합니다.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff0000">
<Button
android:id="@+id/btn_mybutton"
android:layout_height="wrap_content"
android:layout_width="124dip"
android:layout_marginTop="5dip"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
상대 레이아웃에 CENTER_IN_PARENT를 사용할 수 있습니다.
android:layout_centerInParent="true"
RelativeLayout의 중앙에 배치하려는 요소에 추가
아카디아, 아래 코드를 사용해보십시오. 원하는 결과를 얻을 수있는 몇 가지 방법이 있습니다. 이것은 쉬운 방법 중 하나입니다.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relative_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<Button
android:id="@+id/the_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Centered Button"/>
</RelativeLayout>
RelativeLayout 자체의 중력을 설정하면 그 안에 배치 된 모든 개체에 영향을줍니다. 이 경우에는 버튼 일뿐입니다. 물론 여기서 중력 설정을 사용할 수 있습니다 (예 : center_horizontal, top, right 등).
아래 코드를 사용할 수도 있습니다 :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relative_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/the_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Centered Button"
android:layout_centerInParent="true"/>
</RelativeLayout>
요약
첨가:
android:layout_centerInParent="true"
just works on RelativeLayout, if one of the following attributes is also set for the view:
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
which alignes the child view to the parent view. The "center" is based on the axis of the alignement you have chosen:
left/right -> vertical
top/bottom -> horizontal
Setting the gravity of childs/content inside the view:
android:gravity="center"
is centering the child inside the parent view in any case, if there is no alignment set. Optional you can choose:
<!-- Axis to Center -->
android:gravity="center_horizontal"
android:gravity="center_vertical"
<!-- Alignment to set-->
android:gravity="top"
android:gravity="bottom"
android:gravity="left"
android:gravity="right"
android:gravity="fill"
...
Then there is:
android:layout_gravity="center"
which is centering the view itself inside it's parent
And at last you can add the following attribute to the parents view:
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
Use the attribute android:centerInParent="true"
inside a view , when you want to center the view in Relative layout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NWE"
android:textSize="30dp"/>
</RelativeLayout>
Its easy, dont Align it to anything
<Button
android:id="@+id/the_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Centered Button"/>
To center align in one of the direction use android:layout_centerHorizontal="true" or android:layout_centerVertical="true" in the child layout
Removing any alignment like android:layout_alignParentStart="true" and adding centerInParent worked for me. If the "align" stays in, the centerInParent doesn't work
It's really easy. Try the code below,
<RelativeLayout
android:id="@+id/second_RL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/first_RL"
android:background="@android:color/holo_blue_bright"
android:gravity="center">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</RelativeLayout>
you must have aligned it to left or right of parent view
don't use any of these when using android:centerInParent="true"
codes:-
android:layout_alignParentLeft="true"
android:layout_alignParentLeft="true"
android:layout_alignRight=""
etc.
참고URL : https://stackoverflow.com/questions/3748255/can-you-center-a-button-in-relativelayout
'Programming' 카테고리의 다른 글
쉘 명령에서 데이터베이스를 작성하는 방법은 무엇입니까? (0) | 2020.06.01 |
---|---|
CSS3 미디어 쿼리가 모바일 장치에서 작동하지 않는 이유는 무엇입니까? (0) | 2020.06.01 |
명령 줄에서 Windows EventLog 소스를 만드는 방법은 무엇입니까? (0) | 2020.06.01 |
Java에서 배열 정렬 (0) | 2020.06.01 |
C #에서 술어는 무엇입니까? (0) | 2020.06.01 |