2017-04-24 54 views
-1

兩個按鈕側我有我的應用程序的簡單設計:(其我的第一次...;)) enter image description hereAndroid的佈局:有並排

所以,你可以看到它只是一個背景和兩個鈕釦。 (有2個更多的按鈕,目前它們並不重要)

所以,我不知道如何把它們放在另一個旁邊,他們所做的只是彼此重疊。我嘗試重量,但沒有奏效。

這是我axml文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/LinearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:orientation="vertical" 
    android:minWidth="25px" 
    android:minHeight="25px" 
    android:background="@drawable/background"> 
    <Button 
     android:background="@drawable/startthehatching_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:id="@+id/StartButton" /> 
    <Button 
     android:background="@drawable/stopthehatching_button" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:id="@+id/StopButton" /> 
    <CheckBox 
     android:text="Pro Mode On" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/CheckBoxProMode" /> 
    <Button 
     android:text="Help!" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/HelpButton" /> 
</LinearLayout> 

我能不能得到一些幫助?它可能超級容易,但我是新來的:)

謝謝!

+0

做出的LinearLayout水平的方向,因爲按鈕水平對齊 –

+0

你可以嘗試的RelativeLayout代替的LinearLayout ..它好得多 –

+0

這是在所有手機上看起來都一樣嗎? – user7285912

回答

1

創建RelativeLayoutroot佈局。由於您的設計包含bottom-center對齊的CheckboxButton,所以最好使用RelativeLayout作爲container/root佈局。

2.添加一個直接的兒童水平LinearLayout和這個佈局包含2個子女RelativeLayout和兩者有相等的寬度。

3.地點STARTSTOP按鈕各自RelativeLayout

添加attribute android:layout_centerVertical="true"到按鈕STARTSTOP對準vertically裏面的孩子RelativeLayout

4.添加two不同的背景colortwo孩子RelativeLayout而不是使用背景imageroot佈局。

FYI,當我用了兩個RelativeLayout's爲兩個部分,所以它能夠更好地使用背景color而不是使用image

試試這個:

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal" 
     android:weightSum="2"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="#16A25B" 
      android:layout_weight="1" 
      android:padding="20dp"> 

      <Button 
       android:id="@+id/StartButton" 
       android:layout_width="180dp" 
       android:layout_height="180dp" 
       android:layout_centerVertical="true" 
       android:background="@drawable/startthehatching_button" /> 

     </RelativeLayout> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="#16909D" 
      android:layout_weight="1" 
      android:padding="20dp"> 

      <Button 
       android:id="@+id/StopButton" 
       android:layout_width="180dp" 
       android:layout_height="180dp" 
       android:layout_centerVertical="true" 
       android:background="@drawable/stopthehatching_button" /> 

     </RelativeLayout> 
    </LinearLayout> 

    <Button 
     android:id="@+id/HelpButton" 
     android:text="Help!" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="20dp" /> 

    <CheckBox 
     android:id="@+id/CheckBoxProMode" 
     android:text="Pro Mode On" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_above="@id/HelpButton" 
     android:layout_marginBottom="8dp" 
     android:textColor="@android:color/white" /> 
</RelativeLayout> 

僅供參考,您應該定義的Buttonheight-weight成不同dimens.xml所有resolutions針對所有不同的設備完全一樣。

OUTPUT:

enter image description here

希望這將有助於〜

+0

哇真棒! 100%:)))) – user7285912

+0

高興地幫助你:) – FAT

+0

再次感謝!我還有一個要問的問題,你是否想介紹一下你做了些什麼以及它是如何工作的?我的意思是非常基本的,我知道大部分的事情,但例如,我不明白爲什麼你爲某些東西設置背景顏色...你會這樣做嗎?那將是真棒!! – user7285912

0

方向更改爲水平和使用是0dp

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/LinearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/background" 
    android:gravity="center" 
    android:minHeight="25px" 
    android:minWidth="25px" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal" 
     android:weightSum="2"> 

     <FrameLayout 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"> 

      <Button 
       android:id="@+id/StartButton" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:background="@drawable/startthehatching_button" /> 
     </FrameLayout> 

     <FrameLayout 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"> 

      <Button 
       android:id="@+id/StopButton" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 

       android:background="@drawable/stopthehatching_button" /> 
     </FrameLayout> 
    </LinearLayout> 
</LinearLayout> 
+0

嘿謝謝!我做到了這一點,並開始彼此相鄰。但它們不再圓形xD它們變成橢圓形並且變大 - – user7285912

+0

@ user7285912可以將按鈕包裝在框架佈局中,並在按鈕上添加margin =「parent_center」,並在框架佈局上添加width = 0dp –

+0

我不dont真的得到它,atm這是結果:http://imgur.com/a/rslCy – user7285912

0
this code help you!!! 

    <?xml version="1.0" encoding="utf-8"?> 
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/LinearLayout1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center" 
      android:orientation="vertical" 
      android:minWidth="25px" 
      android:minHeight="25px" 
      android:background="@drawable/background"> 
     <LinearLayout 
     android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:weightSum="2" 
      android:orientation="horizontal"> 

      <Button 
       android:background="@drawable/startthehatching_button" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:id="@+id/StartButton" /> 
      <Button 
       android:background="@drawable/stopthehatching_button" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:id="@+id/StopButton" /> 
      </LinearLayout> 
      <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:weightSum="2" 
      android:orientation="horizontal"> 
      <CheckBox 
      android:text="Pro Mode On" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:id="@+id/CheckBoxProMode" /> 
      <Button 
      android:text="Help!" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:id="@+id/HelpButton" /> 

      </LinearLayout> 
     </LinearLayout> 
+0

謝謝你的幫助。但它沒有工作。我認爲問題在於下面有兩個按鈕。無論如何,在這種佈局莫名其妙地這兩個按鈕沒有不同的大小... – user7285912

+0

這是它看起來像atm:http://imgur.com/a/rslCy – user7285912

+0

檢查現在!我改變了代碼!@ user7285912 – kdblue