【问题标题】:Listview and a button in the bottom of the screenListview 和屏幕底部的按钮
【发布时间】:2014-05-26 08:01:10
【问题描述】:

我什么都试过了,到处都在看..!

我正在尝试在滚动视图中放置一个列表视图,并修复了屏幕底部的一个按钮.. 这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/rlCommento" /> 

    <RelativeLayout
        android:id="@+id/rlCommento"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true" >

        <EditText
            android:id="@+id/etCCommento"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_weight="15"
            android:ems="10" >

            <requestFocus />
        </EditText>

        <Button
            android:id="@+id/bCInviaCommento"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/etCCommento"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_weight="85"
            android:text="Commenta!" />

    </RelativeLayout>

</RelativeLayout>

在项目中似乎可以,但是当我在模拟器上尝试时,它不起作用! 有什么建议吗?谢谢!

【问题讨论】:

  • 滚动视图中的 ListView 坏主意
  • 滚动视图在哪里??我根本看不到滚动视图

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


【解决方案1】:

这是你必须做的:

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

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:text="Button" />

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

</ListView>

</RelativeLayout>

【讨论】:

    【解决方案2】:

    您可以使用线性布局。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
    
            android:layout_weight="1"/>
    
        <RelativeLayout
            android:id="@+id/rlCommento"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
    
            ...
    
        </RelativeLayout>
    
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      第一:不要将列表视图放在滚动视图中 第二:使您最上面的父宽度 n 高度与父匹配 第三:在底部添加一个新布局,并在这个布局中添加按钮,像这样,给布局一些权重

      <LinearLayout
                  android:id="@+id/linearLayout1"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_centerInParent="true"
                  android:layout_weight="1"
                  android:orientation="horizontal"
                  android:paddingBottom="7dp"
                  android:paddingTop="7dp" >
      
                  <Button
                      android:id="@+id/score_board"
                      android:layout_width="wrap_content"
                      android:layout_height="55dp"
                      android:layout_gravity="center"
                      android:layout_weight="1"
                      android:text="OK" />
              </LinearLayout>
      

      【讨论】:

        【解决方案4】:
        // Try this way,hope this will help you to solve your problem...
        
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:padding="5dp">
        
            <ListView
                android:id="@android:id/list"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="1"/>
        
            <LinearLayout
                android:id="@+id/rlCommento"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp">
        
                <EditText
                    android:id="@+id/etCCommento"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:ems="10" >
        
                    <requestFocus />
                </EditText>
        
                <Button
                    android:id="@+id/bCInviaCommento"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:text="Commenta!" />
        
            </LinearLayout>
        
        
        </LinearLayout>
        

        【讨论】:

          【解决方案5】:

          为此,您必须使用 LinearLayout 垂直而不是 RelativeLayout 并设置 ListView weight=1 和 layout_height=0dp。 这应该可以根据需要工作:

          <?xml version="1.0" encoding="utf-8"?>
          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical" >
          
          <ListView
              android:id="@android:id/list"
              android:layout_width="fill_parent"
              android:layout_height="0dp"
              android:layout_weight="1" /> 
          
          <RelativeLayout
              android:id="@+id/rlCommento"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content" >
          
              <EditText
                  android:id="@+id/etCCommento"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignParentLeft="true"
                  android:layout_alignParentTop="true"
                  android:layout_weight="15"
                  android:ems="10" >
          
                  <requestFocus />
              </EditText>
          
              <Button
                  android:id="@+id/bCInviaCommento"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignBottom="@+id/etCCommento"
                  android:layout_alignParentRight="true"
                  android:layout_alignParentTop="true"
                  android:layout_weight="85"
                  android:text="Commenta!" />
          
          </RelativeLayout>
          

          【讨论】:

            【解决方案6】:

            希望我正确理解了您的问题。无需将 ListView 保留在滚动视图中。只需尝试跟随 sn-p。它可能适合你。

                <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
            
            <ListView
                    android:id="@android:id/list"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                     /> 
            
            <Button
                        android:id="@+id/bCInviaCommento"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:text="Commenta!"
                        android:layout_below="@id/list" />
            
            </RelativeLayout?
            

            【讨论】:

              猜你喜欢
              • 2012-03-08
              • 1970-01-01
              • 1970-01-01
              • 2013-08-19
              • 2020-06-27
              • 2021-06-17
              • 1970-01-01
              • 1970-01-01
              • 2023-01-30
              相关资源
              最近更新 更多