2016-12-25 27 views
0

我正在嘗試使用3個按鈕與中心對齊進行線性佈局。Android-按鈕的一部分從佈局中刪除

但是,在設置了一些重量並將空間置於其間之後,最後一個按鈕的佈局中有1/4被刪除,不可見並且不可點擊。其他部分雖然工作得很好。

對不起它的一個新的帳戶,所以我不能嵌入的截圖,而不是請點擊以下鏈接:

Here is a picture of the apps

,並請另見下文

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
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:layout_gravity="center" 
android:orientation="horizontal" 
android:weightSum="4"> 
<Space 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="1"/> 
<!--Nesting Structured Layout--> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:weightSum="10" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:orientation="vertical" 
    android:layout_weight="2"> 

    <!--This is for the Start button--> 
    <Button 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="3" 
     android:text="@string/starting_button"/> 
    <!--Spacing--> 
    <Space 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"/> 
    <!--This is for the Message button--> 
    <Button 
     android:layout_weight="3" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:text="@string/liitle_message_button"/> 
    <!--Spacing--> 
    <Space 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"/> 
    <!--This is for the Settings button--> 
    <Button 
     android:layout_weight="3" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:text="@string/settings_button"/> 
</LinearLayout> 
<Space 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="1"/> 
</LinearLayout> 

什麼碼導致問題的代碼問題?還有其他更好的方法來達到相同的結果嗎?

P.S對不起,作爲一個非英語母語的用戶,我很抱歉,它可能有大量的語法錯誤或以不禮貌的方式提問,請讓我知道你是否感到冒犯。

回答

0

由於您使用weightSum分配高度,高度固定在「的LinearLayout」,所以你的最後一個按鈕就走出了固定高度的。

您的問題是通過從內部LinearLayout中簡單地刪除android:weightSum="10"來解決的。你可以閱讀更多關於weightSum here

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="center" 
    android:orientation="horizontal" 
    android:weightSum="4"> 
    <Space 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1"/> 
    <!--Nesting Structured Layout--> 
    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:orientation="vertical" 
     android:layout_weight="2"> 

     <!--This is for the Start button--> 
     <Button 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="3" 
      android:text="b1"/> 
     <!--Spacing--> 
     <Space 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1"/> 
     <!--This is for the Message button--> 
     <Button 
      android:layout_weight="3" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:text="b2"/> 
     <!--Spacing--> 
     <Space 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1"/> 
     <!--This is for the Settings button--> 
     <Button 
      android:layout_weight="3" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:text="b3"/> 
    </LinearLayout> 
    <Space 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1"/> 
</LinearLayout> 

而不是使用space您可以使用android:gravityandroid:layout:gravity中心的佈局。

同樣使用嵌套權重不利於性能。

+0

我既然對不起我遲到的答覆 – Noobnewbier

+0

我是新來的stackoverflow,我噸希望我會有電子郵件每次我有一個答案......但似乎它不是它的工作方式TAT – Noobnewbier

+0

沒問題,你的問題解決了嗎? – Avin

0

這是因爲weightSum在LinearLayout中的重量總和爲10,但是當您在LinearLayout中添加按鈕和空間的權重值時,結果爲11,因此第二個LinearLayout中的weightSum必須爲11. Here您有一個問題,讓更多的澄清weightSum。 只是你的代碼必須是這樣的。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
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:layout_gravity="center" 
android:orientation="horizontal" 
android:weightSum="4"> 
<Space 
android:layout_width="0dp" 
android:layout_height="match_parent" 
android:layout_weight="1"/> 
<!--Nesting Structured Layout--> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:weightSum="11" 
android:layout_width="0dp" 
android:layout_height="wrap_content" 
android:layout_gravity="center" 
android:orientation="vertical" 
android:layout_weight="2"> 

<!--This is for the Start button--> 
<Button 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="3" 
    android:text="@string/starting_button"/> 
<!--Spacing--> 
<Space 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1"/> 
<!--This is for the Message button--> 
<Button 
    android:layout_weight="3" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:text="@string/liitle_message_button"/> 
<!--Spacing--> 
<Space 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1"/> 
<!--This is for the Settings button--> 
<Button 
    android:layout_weight="3" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:text="@string/settings_button"/> 
</LinearLayout> 
<Space 
android:layout_width="0dp" 
android:layout_height="match_parent" 
android:layout_weight="1"/> 
</LinearLayout> 
0

不要讓它很複雜,您可以通過使用相對佈局及線性佈局

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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:orientation="horizontal"> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:orientation="vertical"> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:gravity="center" 
      android:text="START" /> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:layout_marginTop="10dp" 
      android:text="LITTLE MESSAGE" /> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:text="SETTINGS" /> 
    </LinearLayout> 
</RelativeLayout> 
+0

我想這將是一個更好的諒解備忘錄 – Noobnewbier

+0

感謝很多隊友,我會試試這 – Noobnewbier

+0

@Noobnewbier謝謝,請upvote,如果你喜歡答案夥伴 – Naga

0

這是佈局非常容易地實現它..

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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:orientation="horizontal"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:weightSum="9" 
     android:orientation="vertical"> 

     <Button 
      android:layout_weight="3" 
      android:layout_width="wrap_content" 
      android:layout_height="0dp" 
      android:layout_margin="10dp" 
      android:text="START" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_weight="3" 
      android:layout_height="0dp" 
      android:layout_margin="10dp" 
      android:text="MIDDLE" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_weight="3" 
      android:layout_height="0dp" 
      android:layout_margin="10dp" 
      android:text="LAST" /> 

    </LinearLayout> 
</RelativeLayout> 
    Attaching image also 

enter image description here

+0

thx爲您的答案! – Noobnewbier

+0

對不起,我晚回覆壽,我的問題解決了,由於你們的努力 – Noobnewbier

0

使用這個簡化的代碼來實現相同的結果:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:orientation="vertical" 
    android:padding="50dp"> 

    <!--This is for the Start button--> 
    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/starting_button" /> 

    <!--This is for the Message button--> 
    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" 
     android:text="@string/liitle_message_button" /> 

    <!--This is for the Settings button--> 
    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" 
     android:text="@string/settings_button" /> 

</LinearLayout> 

見下圖:

enter image description here

+0

謝謝! 我增加了空間,以確保他們每個人都會有相同的寬度 – Noobnewbier

+0

不知道是最佳做法 – Noobnewbier