簡體   English   中英

Android:如何創建這個簡單的布局?

[英]Android: How to create this simple layout?

我需要創建一個帶有兩個表單小部件的簡單布局(例如 - 兩個按鈕)。 第一個必須填充父布局的所有可用寬度,第二個必須具有一些固定大小。

這就是我需要的: 在此輸入圖像描述

如果我將FILL_PARENT設置為第一個小部件 - 我沒有看到第二個小部件。 它只是從布局的視圖區域吹走:)我不知道如何解決這個問題...

最簡單的方法是使用layout_weightLinearLayout 注意第一個TextView的寬度是"0dp" ,這意味着“忽略我並使用權重”。 重量可以是任意數量; 因為它是唯一的加權視圖,所以它將擴展以填充可用空間。

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        />
    <TextView
        android:layout_width="25dp"
        android:layout_height="wrap_content"
        />
</LinearLayout>

您可以使用RelativeLayout或FrameLayout來實現這一點。

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="5dp"
        android:background="#ccccee"
        android:text="A label. I need to fill all available width." />

    <TextView
        android:layout_width="20dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:paddingRight="5dp"
        android:background="#aaddee"
        android:text=">>" />

</RelativeLayout>

布局看起來如何

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM