【问题标题】:Cannot see second button看不到第二个按钮
【发布时间】:2014-04-21 07:52:31
【问题描述】:

我正在尝试在我的主屏幕上创建一个菜单,但我看不到我创建的第二个按钮。我试过android:layout_below="@+id/score_button",但它给出了警告“无效的布局参数”

这是我的 xml 文件

<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:orientation="horizontal" >

    <Button
        android:id="@+id/score_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="scoreGame"
        android:text="@string/score_button" />

    <Button
        android:id="@+id/settings_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/action_settings" />

</LinearLayout>

我注意到,xml 文件中最先放置的按钮就是我在设备上运行应用程序时显示的那个按钮。

提前感谢您的帮助,非常感谢。

【问题讨论】:

  • 尝试设置android:layout_width="wrap_content"..!!!!
  • android:orientation="vertical"
  • 并将按钮宽度设置为 android:layout_width="wrap_content"

标签: android android-layout android-linearlayout android-view


【解决方案1】:

使用 RelativeLayout 而不是 LinearLayout 那么你可以使用android:layout_below="@+id/score_button"

并在使用RelativeLayout时删除android:orientation="horizontal"

【讨论】:

    【解决方案2】:

    将 layout_width 更改为 wrap_content:

    <Button
        android:id="@+id/score_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="scoreGame"
        android:text="@string/score_button" />
    
    
    <Button
        android:id="@+id/settings_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/action_settings" />
    

    另外,如果你想要等宽按钮,请给出 layout_weight:

    android:layout_weight="1"
    

    两个按钮。

    【讨论】:

      【解决方案3】:

      试试下面的代码:-

      <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:orientation="horizontal" >
      
          <Button
              android:id="@+id/score_button"
              android:layout_width="50dp"
              android:layout_height="wrap_content"
              android:onClick="scoreGame"
              android:text="@string/score_button" />
      
          <Button
              android:id="@+id/settings_button"
              android:layout_width="50dp"
              android:layout_height="wrap_content"
              android:text="@string/action_settings" />
      
      </LinearLayout>
      

      【讨论】:

        【解决方案4】:

        用这段代码替换你的 xml 代码...

         <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:orientation="horizontal" >
        
        <Button
            android:id="@+id/score_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="scoreGame"
            android:text="@string/score_button" />
        
        <Button
            android:id="@+id/settings_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/action_settings" />
        
        </LinearLayout>
        

        【讨论】:

          【解决方案5】:

          用户方向为垂直。在线性布局中没有 layout_below 属性。请改用相对布局。

          【讨论】:

            【解决方案6】:

            强制两个按钮的宽度相同:

            <Button
                android:layout_width="0dp"
                android:layout_weight="1"
                ... />
            
            <Button
                android:layout_width="0dp"
                android:layout_weight="1"
                ... />
            

            使用自然按钮宽度:

            <Button
                android:layout_width="wrap_content"
                ... />
            
            <Button
                android:layout_width="wrap_content"
                ... />
            

            【讨论】:

              【解决方案7】:

              宽度用“match_parent”代替,放权重

                <Button
                      android:id="@+id/score_button"
                      android:layout_width="0dp"
                      android:layout_weight="1"
                      android:layout_height="wrap_content"
                      android:onClick="scoreGame"
                      android:text="@string/score_button" />
              
                  <Button
                      android:id="@+id/settings_button"
                      android:layout_width="0dp"
                      android:layout_weight="1"
                      android:layout_height="wrap_content"
                      android:text="@string/action_settings" />
              

              【讨论】:

                【解决方案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:orientation="horizontal" >
                
                    <Button
                        android:id="@+id/score_button"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:onClick="scoreGame"
                        android:layout_weight="1"
                        android:text="@string/score_button" />
                
                    <Button
                        android:id="@+id/settings_button"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="@string/action_settings" />
                
                </LinearLayout>
                

                如果你想显示一个打击另一个改变方向为垂直

                【讨论】:

                  【解决方案9】:

                  试试这个..

                  将此方向horizontal更改为vertical,如下所示

                  android:orientation="horizontal" 
                  

                  android:orientation="vertical" 
                  

                  【讨论】:

                    【解决方案10】:

                    适当调整按钮的宽度和高度并再次检查..一切顺利

                    【讨论】:

                      【解决方案11】:

                      android:layout_below="" 属性只能与 相对布局 一起使用。您选择了线性布局

                      另外两个按钮都为android:layout_width 属性选择“match_parent”。

                      要么将“match_parent”改为“wrap_content”,否则将线性布局的android:orientation标签改为“vertical”。

                      此外,如果您将布局更改为相对,它将起作用。

                      【讨论】:

                      • 请接受任何答案,以免未回答部分显示此问题。
                      猜你喜欢
                      • 2022-01-15
                      • 1970-01-01
                      • 1970-01-01
                      • 2022-07-06
                      • 1970-01-01
                      • 1970-01-01
                      • 2015-09-30
                      • 1970-01-01
                      • 2017-08-06
                      相关资源
                      最近更新 更多