상대 배치에서 단추의 중심을 잡을 수 있습니까?
버튼을 상대적인 레이아웃에 가운데로 맞추려고 하는데, 가능한가요?그래비티와 오리엔테이션 기능을 사용해 보았지만 아무 것도 작동하지 않습니다.
해라
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"
상대 배치에서 중심을 잡을 요소
아카디아, 아래 코드를 사용해 보세요.원하는 결과를 얻을 수 있는 몇 가지 방법이 있는데, 이 방법이 가장 쉬운 방법 중 하나입니다.
<?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>
상대 배치 자체의 중력을 설정하면 배치된 모든 개체에 영향을 미칩니다.이 경우에는 버튼만 있으면 됩니다.물론 여기서 임의의 중력 설정(예: 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"
보기에 대해 다음 특성 중 하나도 설정된 경우 RelativeLayout(상대 레이아웃)에서만 작동합니다.
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
하위 뷰를 상위 뷰에 정렬합니다."중심"은 선택한 선형의 축을 기준으로 합니다.
왼쪽/오른쪽 -> 세로
상/하의 ->
뷰 내부에서 하위/내용의 중력 설정:
android:gravity="center"
는 정렬 세트가 없는 경우 어떤 경우에도 자식을 부모 보기 내부로 중심을 맞춥니다.선택사항:
<!-- 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"
...
그 다음은 다음과 같습니다.
android:layout_gravity="center"
시각 자체를 부모 안에 중심을 두고 있는 거죠
그리고 마침내 다음 속성을 부모 보기에 추가할 수 있습니다.
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
그건 쉬워요, 어떤 것에도 맞추지 마세요.
<Button
android:id="@+id/the_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Centered Button"/>
속성 사용android:centerInParent="true"
뷰 내부, 상대 레이아웃에서 뷰의 중심을 맞추려는 경우.
<?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>
한 방향으로 중앙 정렬하려면 하위 레이아웃에서 Android:layout_centerHorizontal="true" 또는 Android:layout_centerVertical="true"를 사용합니다.
진짜 쉬워요.아래 코드를 사용해 보십시오.
<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>
아주 간단해요, 그냥 사용하세요.Android:CenterInParent="true"
수평과 수직 양쪽에 가운데를 두거나 사용하는Android:Horizontal="true"
수평으로 중심을 잡다Android:Vertical="true"
그리고 이 모든 것을 릴레이티브 레이아웃에 적어야 합니다.
Android:layout_alignParentStart="true"와 같은 정렬을 제거하고 centerInParent를 추가하는 것이 효과적이었습니다."정렬"이 계속 유지되면 centerInParent가 작동하지 않습니다.
부모 뷰의 왼쪽 또는 오른쪽에 정렬되어 있어야 합니다.
사용할 때는 이 중 하나도 사용하지 마십시오.android:centerInParent="true"
드:-
android:layout_alignParentLeft="true"
android:layout_alignParentLeft="true"
android:layout_alignRight=""
기타.
언급URL : https://stackoverflow.com/questions/3748255/can-you-center-a-button-in-relativelayout
'programing' 카테고리의 다른 글
liquibase가 있는 Java에서 오래된 데이터베이스 스키마를 새 데이터베이스 스키마로 업데이트하는 방법 (0) | 2023.10.14 |
---|---|
워드프레스에 프로그래밍적으로 업로드하는 방법 - 파일 저장 문제 (0) | 2023.10.14 |
맥 OS X 10.7 (Lion)에서 나노 구문 강조? (0) | 2023.10.14 |
HttpContext에 접근합니다.서로 다른 스레드의 전류 (0) | 2023.10.09 |
구조에서 ->와 .의 차이? (0) | 2023.10.09 |