2016-12-25 56 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