簡體   English   中英

是否可以將按鈕作為圖層放置在表格布局的頂部?

[英]Is it possible to position a button on top of a table layout as a layer?

我將四個按鈕緊密地綁在一起以形成一個正方形,我正在努力找出如何將一個按鈕放置在四個按鈕的中間,以便使其基本上與它們重疊。 這是我當前的帶有四個按鈕的布局:

<TableRow android:layout_weight="0.2" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|center"
        android:layout_span="2"
        android:gravity="center"
        android:text="TextView"
        android:textColor="#357ae8"
        android:textSize="20dp"
        android:textStyle="bold" />
</TableRow>

<TableRow
    android:layout_gravity="center_horizontal"
    android:layout_weight="1" >

    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_column="0"
        android:layout_marginBottom="-7dp"
        android:layout_marginLeft="5dp"
        android:layout_weight="1"
        android:background="@drawable/b1tl"
        android:gravity="center_horizontal" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_column="0"
        android:layout_marginBottom="-7dp"
        android:layout_marginRight="5dp"
        android:layout_weight="1"
        android:background="@drawable/b2tr"
        android:gravity="center_horizontal" />
</TableRow>

<TableRow
    android:layout_margin="0dp"
    android:layout_weight="1" >

    <Button
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_column="0"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="-7dp"
        android:layout_weight="1"
        android:background="@drawable/b3bl"
        android:gravity="center_horizontal" />

    <Button
        android:id="@+id/button4"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_column="0"
        android:layout_marginRight="5dp"
        android:layout_marginTop="-7dp"
        android:layout_weight="1"
        android:background="@drawable/b4br"
        android:gravity="center_horizontal" />
</TableRow>

<TableRow
    android:layout_margin="10dp"
    android:layout_weight="0.1" >

    <TextView
        android:id="@+id/textView4"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_column="0"
        android:layout_weight="1"
        android:gravity="center_horizontal"
        android:text="Lives"
        android:textColor="#357ae8" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_column="0"
        android:layout_weight="1"
        android:gravity="center_horizontal"
        android:text="Score"
        android:textColor="#357ae8" />
</TableRow>

<TableRow
    android:layout_margin="10dp"
    android:layout_weight="0.1" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_column="0"
        android:layout_weight="1"
        android:gravity="center_horizontal"
        android:text=""
        android:textColor="#357ae8" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_column="0"
        android:layout_weight="1"
        android:gravity="center_horizontal"
        android:text=""
        android:textColor="#357ae8" />
</TableRow>

</TableLayout>

我當時想我可以使用相對的布局,然后嘗試對按鈕進行硬編碼,使其位於按鈕正方形的中心,但是,一旦我嘗試這樣做,它就會影響表的布局。 甚至有可能在Android布局中執行這種操作嗎?

嗨,可以使用FrameLayout。 這是完成的代碼,其中一個按鈕位於行中兩個按鈕之間的中央位置。 同樣,您可以解決將一個按鈕放置在所有四個按鈕中間的位置。 這是我的操作方法:在表格行中>一個FrameLayout。 在FrameLayout內部,將LinearLayout和Button作為子元素,以便它們both can overlap (framelayout的屬性)。 這樣,我們放置在LinearLayout中的所有內容也將與上一個按鈕分層。


請檢查此代碼。 我認為這是您想要的:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TableLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <TableRow android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="0.2" > <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal|center" android:layout_span="2" android:gravity="center" android:text="TextView" android:textColor="#357ae8" android:textSize="20dp" android:textStyle="bold" /> </TableRow> <TableRow android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center_horizontal" android:layout_weight="1" > <FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:layout_weight="1" > <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginBottom="-7dp" android:layout_marginLeft="5dp" android:layout_weight="1" android:background="@drawable/ic_launcher" android:gravity="center_horizontal" /> <Button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginBottom="-7dp" android:layout_marginRight="5dp" android:layout_weight="1" android:background="@drawable/ic_launcher" android:gravity="center_horizontal" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:layout_weight="1" > <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginBottom="-7dp" android:layout_marginLeft="5dp" android:layout_weight="1" android:background="@drawable/ic_launcher" android:gravity="center_horizontal" /> <Button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginBottom="-7dp" android:layout_marginRight="5dp" android:layout_weight="1" android:background="@drawable/ic_launcher" android:gravity="center_horizontal" /> </LinearLayout> </LinearLayout> <Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="150dp" android:layout_gravity="center" android:text="Center Button" /> </FrameLayout> </TableRow> <TableRow android:layout_margin="10dp" android:layout_weight="0.1" > <TextView android:id="@+id/textView4" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_column="0" android:layout_weight="1" android:gravity="center_horizontal" android:text="Lives" android:textColor="#357ae8" /> <TextView android:id="@+id/textView5" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_column="0" android:layout_weight="1" android:gravity="center_horizontal" android:text="Score" android:textColor="#357ae8" /> </TableRow> <TableRow android:layout_margin="10dp" android:layout_weight="0.1" > <TextView android:id="@+id/textView2" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_column="0" android:layout_weight="1" android:gravity="center_horizontal" android:text="" android:textColor="#357ae8" /> <TextView android:id="@+id/textView3" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_column="0" android:layout_weight="1" android:gravity="center_horizontal" android:text="" android:textColor="#357ae8" /> </TableRow> </TableLayout> </RelativeLayout> 

我可能會缺少一些東西,但是...

如果將當前布局放在RelativeLayout ,將其與頂部,底部,左側和右側對齊,該怎么辦。

確認沒有任何更改,並且仍然可以正常使用。

然后在此RelativeLayout內放置一個按鈕,並將其對齊到此RelativeLayout內的中心。

我沒有方便的Eclipse檢查它,但是它應該就是這么簡單,不是嗎?


即非常@durgomgiri在他的回答中說。

暫無
暫無

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

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