Android LinearLayout
-
12-09-2019 - |
문제
두 개의 텍스트 뷰를 왼쪽에 놓고 선형 레이아웃 내부의 오른쪽에 하나의 버튼을 놓고 싶습니다. 이것이 가능합니까? 다음은 버튼의 왼쪽 마진을 하드 코딩 해야하는 코드입니다. 이것은 융통성이 없습니다. 다른 방향으로 흐르는 어린이를 레이아웃 할 수 있습니까?
<LinearLayout
android:id="@+id/widget43"
android:layout_width="fill_parent"
android:layout_height="100px"
>
<TextView
android:id="@+id/tc_buttom_text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time elapsed"
>
</TextView>
<TextView
android:id="@+id/tc_buttom_text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00:00 00"
>
</TextView>
<Button
android:id="@+id/tc2_home"
android:layout_width="70px"
android:layout_height="wrap_content"
android:layout_marginLeft="200px"
android:layout_marginRight="10px"
android:text="Home"
android:layout_weight="0.5"
>
</Button>
</LinearLayout>
해결책
두 개의 텍스트 뷰를 왼쪽에 놓고 선형 레이아웃 내부의 오른쪽에 하나의 버튼을 놓고 싶습니다. 이것이 가능합니까?
싱글이 아닙니다 LinearLayout
. 둘 중 하나가 필요합니다 LinearLayout
s (하나는 두 열입니다 TextView
왼쪽에있는 s) 또는 하나의 Releverivelayout.
다른 방향으로 흐르는 어린이를 레이아웃 할 수 있습니까?
"다른 방향"으로 수직 및 수평을 동시에 의미하는 경우 LinearLayout
한 방향으로 만 갈 수 있습니다. 중첩을 사용하십시오 LinearLayout
s 또는 a RelativeLayout
.
다른 팁
두 개의 선형 레이아웃 (수평 방향이 있고 수직 방향이있는 하나)을 사용하거나 상대 레이아웃을 사용하십시오. 상대 레이아웃은 선형보다 강하고 사용하기 쉽습니다.
테이블 레이아웃을 사용해야합니다. API 데모의 테이블 레이아웃 예제를 참조하십시오.
Table layout - with 'stretch columns' = 1,
-- Table row - with width = fill_parent,
-- Text View,
-- Text View,
-- Button,
이것은 오른쪽 버튼을 화면의 오른쪽 가장자리로 밀어 넣을 수 있습니다.
LinearLayout 있습니다 orientation
기인하다. 다음과 같은 것을 시도하십시오.
<LinearLayout
android:id="@+id/widget43"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="100px"
>
다음 사용 :
<LinearLayout
android:id="@+id/widget43"
android:layout_width="fill_parent"
android:layout_height="100px"
android:layout_margin="16dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tc_buttom_text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time elapsed"></TextView>
<TextView
android:id="@+id/tc_buttom_text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="00:00:00 00"></TextView>
</LinearLayout>
<Button
android:id="@+id/tc2_home"
android:layout_width="70px"
android:layout_height="wrap_content"
android:layout_marginLeft="200px"
android:layout_marginRight="10px"
android:layout_weight="0.5"
android:text="Home"></Button>
</LinearLayout>
더 많은 안드로이드 레이아웃 자습서 및 예를 보려면 : http://www.viralandroid.com/2015/11/android-layouts.html
나는 다음과 같은 두 개의 선형 층을 사용합니다.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099cc"
tools:context=".FullscreenActivity">
<!-- The primary full-screen view. This can be replaced with whatever view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc. -->
<TextView
android:id="@+id/fullscreen_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:keepScreenOn="true"
android:text="@string/dummy_content"
android:textColor="#33b5e5"
android:textSize="50sp"
android:textStyle="bold" />
<!-- This FrameLayout insets its children based on system windows using
android:fitsSystemWindows. -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_dark"
android:fitsSystemWindows="true">
<LinearLayout
android:id="@+id/fullscreen_content_controls"
style="?metaButtonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:background="@color/black_overlay"
android:baselineAligned="false"
android:orientation="vertical"
tools:ignore="UselessParent">
<LinearLayout
android:id="@+id/fullscreen_choose_controls"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/fullscreen_esop"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="@string/vs_esop"
android:textAllCaps="false"
android:textColor="@android:color/holo_blue_dark"
android:textSize="50sp"
android:textStyle="bold" />
<Button
android:id="@+id/dummy_button"
style="?metaButtonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.3"
android:ellipsize="end"
android:gravity="center"
android:text="@string/dummy_button"
android:textColor="@android:color/holo_red_dark"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/fullscreen_android"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="@string/vs_android"
android:textAllCaps="false"
android:textColor="@android:color/holo_blue_dark"
android:textSize="50sp"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="@+id/fullscreen_make_your_choice"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="@string/vs_make_your_choice"
android:textColor="@android:color/holo_red_dark"
android:textSize="50sp"
android:textStyle="bold" />
</LinearLayout>
</FrameLayout>
</FrameLayout>