アンドロイドのLinearLayout
-
12-09-2019 - |
質問
私はこれが可能で、リニアレイアウト内で右へ左へ2つのTextViewと、一つのボタンを築くためにしたいですか? 以下では、これは柔軟性に欠けている、私はボタンのLEFTMARGINをハードコーディングしなければならなかった私のコードです。それは、異なる方向に流れる子をレイアウトすることは可能ですか?
<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>
解決
私はへ2つのTextViewを築くためにしたいです 左、右に一つのボタン リニアレイアウトの内側に、これはあります 可能?
未単一LinearLayout
と。あなたはどちらか2 LinearLayout
s(左の2 TextView
sの列に1つ)、または1 RelativeLayoutを必要とします。
「異なる方向」で、あなたが同時に垂直方向と水平方向の両方を意味する場合は、これは、レイアウトの子供たちに可能であること 異なる方向に流れる?
、単一LinearLayout
は一方向にしか行くことができます。いずれかのネストされたLinearLayout
sまたはRelativeLayout
を使用します。
他のヒント
のいずれか2つの線形レイアウト(水平方向と垂直方向を有する他のものを)使用または相対レイアウトを使用します。相対的なレイアウトは、リニアよりも強いと
使いやすいですあなたは表のレイアウトを使用する必要があります。 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>
よりAndroidのレイアウトのチュートリアルと例:ます。http://www.viralandroid。 COM / 11分の2015 /アンドロイド-layouts.htmlする
私はこのような2 LinearLayoutsを使用します
<?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>