其他分享
首页 > 其他分享> > 在android中的相对布局底部需要一个按钮

在android中的相对布局底部需要一个按钮

作者:互联网

我是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

解决方法:

我想你应该这样做:

<?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占用
希望这可以帮助.

标签:android,android-linearlayout,android-button
来源: https://codeday.me/bug/20190831/1778071.html