【问题标题】:Button is not displaying in layout in Android按钮未在 Android 的布局中显示
【发布时间】:2014-10-09 23:37:32
【问题描述】:

我做了一个 Android 活动。在这个活动中,我有一个ListView 和一个Button。我想将Button 添加到屏幕底部。我已经尝试了很长时间,但它从标签栏中隐藏了。我的xml 代码如下。请帮帮我。

代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <com.eps.blancatours.uc.Header
        android:id="@+id/hdr_dest_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" />

    <RelativeLayout
        android:id="@+id/rl_main_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/hdr_dest_list"
        android:padding="10dp" >

        <ListView
            android:id="@+id/dest_List"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_above="@+id/btn_next" />

        <Button
            android:id="@+id/btn_next"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="40dp"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="40dp"
            android:background="#D41016"
            android:text="@string/next"
            android:textColor="#ffffff"
            android:textSize="14dp" />

        </RelativeLayout>
 </RelativeLayout>

【问题讨论】:

  • btn_next中删除android:layout_centerHorizontal="true"
  • @GiruBhai-删除后没有变化..!!!
  • 清理您的项目并重试
  • 尝试改变listview的android:layout_height="wrap_content"
  • @jollz-仍然存在同样的问题..!!! :(

标签: android android-layout android-button android-relativelayout


【解决方案1】:

进行如下更改 -

<?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"
        tools:context=".MainActivity" >

        <LinearLayout android:id="@+id/li"
             android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            >

        <com.eps.blancatours.uc.Header
        android:id="@+id/hdr_dest_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
        </LinearLayout>

       <ListView
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_above="@+id/button1"

           android:id="@+id/list"
           android:padding="30dp"

           />

         <Button
             android:id="@+id/button1"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_alignParentBottom="true"
             android:layout_centerHorizontal="true"
             android:background="#68bdb6"
             android:text="@string/Select" />


    </RelativeLayout>

【讨论】:

  • 奇怪.. 我在线性布局中使用 imageview 而不是标题,并且工作正常。尝试重新启动 eclipse 或清理你的项目一次
【解决方案2】:

你没有关闭你的主Relativelayout只是关闭那个布局按钮将出现在列表视图下方

【讨论】:

  • 实际上他做到了,但@Elango 删除并编辑了原始问题。
【解决方案3】:

您没有使用&lt;/RelativeLayout&gt; 关闭主RelativeLayout。

对我来说,你的代码可以工作,你只需要关闭你的相对布局。

【讨论】:

  • 实际上他做到了,但@Elango 删除并编辑了原始问题
  • 我只是忘了张贴右括号。如果没有括号它怎么能工作..:O
  • 我得到了与@Raviindra Singh 发布的相同的布局。我刚刚删除了 因为我没有这个类,更改了@string/next 并关闭了RelativeLayout。
【解决方案4】:

它在这里工作,我添加的只是父标记之前的 &lt;/RelativeLayout&gt; 标记。 和我的文本代替 @String 参考。 还在渲染布局时尝试更改 android 版本

【讨论】:

    【解决方案5】:

    用嵌套的RelativeLayout 包裹ListViewButton 是多余的。你可以像这样设置你的 XML:

    <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" >
    
        <com.eps.blancatours.uc.Header
            android:id="@+id/hdr_dest_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true" />
        <ListView
            android:id="@+id/dest_List"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:padding="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/hdr_dest_list"
            android:layout_above="@+id/btn_next" />
    
        <Button
            android:id="@+id/btn_next"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="40dp"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="40dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:paddingBottom="10dp"
            android:background="#D41016"
            android:text="@string/next"
            android:textColor="#ffffff"
            android:textSize="14dp" />
    </RelativeLayout>
    

    也可以使用match_parent 代替fill_parent

    【讨论】:

      【解决方案6】:

      在 Button 中添加以下内容

      android:layout_below="@id/list"
      

      【讨论】:

        【解决方案7】:

        在两个 RelativeLayouts 上添加 android:orientation="vertical"

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >
        
            <com.eps.blancatours.uc.Header
                android:id="@+id/hdr_dest_list"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true" />
        
            <RelativeLayout
                android:id="@+id/rl_main_list"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/hdr_dest_list"
                android:padding="10dp"
                android:orientation="vertical" >
        
                <ListView
                    android:id="@+id/dest_List"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_above="@+id/btn_next" />
        
                <Button
                    android:id="@+id/btn_next"
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginBottom="40dp"
                    android:layout_marginLeft="40dp"
                    android:layout_marginRight="40dp"
                    android:background="#D41016"
                    android:text="@string/next"
                    android:textColor="#ffffff"
                    android:textSize="14dp" />
            </RelativeLayout>
        </RelativeLayout>
        

        别忘了清除项目。

        【讨论】:

        • Orientation 由 LinearLayout 使用,而不是 RelativeLayout。
        猜你喜欢
        • 1970-01-01
        • 2018-10-18
        • 1970-01-01
        • 2021-09-27
        • 1970-01-01
        • 2020-12-13
        • 2017-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多