【问题标题】:Need a button at the bottom of a Relative Layout in android在android中的相对布局底部需要一个按钮
【发布时间】:2013-07-04 10:58:06
【问题描述】:

我对 Android 很陌生。我正在创建一个包含可点击文本的应用程序。 所有可点击的文本都存在于一个数组中,该数组随后与 ListView 关联并显示在 RelativeLayout 中。

它的代码是 -

    this.setListAdapter(new ArrayAdapter<String>(this, R.layout.main, R.id.label, contact_name));
    ListView lv = getListView();

main 是相对布局,label 是布局内的文本框 id,contact_name 是作为可点击文本的数组。这一切都很好。

最后当数组非常大时,线性布局变成可滚动的。

现在我想将此列表占用的区域限制为屏幕总高度的 80% 或 90%,并在底部保留空间用于打开新页面/视图的按钮。在相对布局中包含按钮是为列表中的每个项目添加一个按钮。改变相对布局的高度就是改变列表中每个项目的高度。这得出结论,数组中的每个项目都与整个相对布局相关联,并且相对布局数组用于显示所有项目。现在如何通过将相对布局列表限制为屏幕顶部的 80% 来将按钮放在底部。

这是当前 XML 文件的样子

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

    <Button
        android:id="@+id/btn"
        android:layout_alignParentBottom="true" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="Next"
        />

    <ScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:layout_above="@id/btn">

        <TextView
            android:id="@+id/label"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:padding="10dip"
            android:textSize="16sp"
            android:textStyle="bold" >
        </TextView>
    </ScrollView>
</RelativeLayout>

代码结束

输出是一个隐藏实际内容的 Next 按钮数组。

这是我的 Java 代码

// Make the contact number parameter accessible to member functions of List View
        final String[] f_contact_number = contact_number;

        // Binding Array to ListAdapter
        this.setListAdapter(new ArrayAdapter<String>(this, R.layout.main, R.id.label, contact_name));

        ListView lv = getListView();

        // listening to single list item on click
        lv.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view,
              int position, long id) {

              Intent intent = new Intent(Intent.ACTION_CALL);
              intent.setData(Uri.parse(f_contact_number[position]));
              startActivity(intent);

          }
        });

Contact_number 和contact_names 是两个字符串数组,在点击contact_name 时调用一个号码

Thanks

【问题讨论】:

    标签: android android-linearlayout android-button


    【解决方案1】:

    我认为你应该这样做:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <Button
            android:id="@+id/btn"
            android:layout_alignParentBottom="true" 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="Next"
            />
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:layout_above="@id/btn">
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content" 
                android:text="Scroll view content"
                >
            </TextView>
        </ScrollView>
    </RelativeLayout>
    

    所以按钮将粘在父级的底部,剩余空间将被ScrollView占用 希望这会有所帮助。

    【讨论】:

    • 我试过了,但不知何故,按钮显示为列表。 Next 按钮的数量等于数组中元素的数量。根据需要,底部没有一个 Next 按钮。更多在数组中的实际文本是不可见的,也不是可点击的。可能每个 Next 按钮都位于这些文本之上并隐藏它们
    • 您能否编辑您的问题以显示 xml 文件? @user2550128
    • 您的 xml 在我的模拟器中提供了正确的 o/p。你在java文件中做什么来在滚动视图中加载文本? @user2550128
    • 你能检查一下代码并告诉我有什么问题吗?我得到了一系列没有显示联系人的下一步按钮。
    【解决方案2】:

    使用RelativeLayout 更容易做到这一点。如果您想使用LinearLayout 执行此操作,您可以将视图的 layout_height 设置为 0dp 并使用 layout_weight 分配高度 p>

    【讨论】:

    • 你能检查一下代码并告诉我我做错了什么吗?我尝试了很多,但我得到的输出并不理想。
    【解决方案3】:
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" >
    
      <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/yourbtnid"
        android:scrollbars="vertical" >
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="your text" >
        </TextView>
      </ScrollView>
    
      <Button
        android:id="@+id/yourbtnid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="center_vertical"
        android:text="Button" />
    
    </RelativeLayout>
    

    【讨论】:

    • 这段代码和Rapunzel建议的代码本质上是一样的。请查看创建的输出。它有点像 Button Button Button Button 垂直排列,实际元素不可见
    【解决方案4】:

    我按照 Anamika 的建议做了,效果很好。

    我改了一点代码,所以按钮设置在中间,并没有填满所有的空间。

    也许您的问题出在 java 代码中,而不是在布局代码中。

    你让它工作了吗?

    如果您仍然在苦苦挣扎,请告诉我,我已经查看并帮助您解决。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多