2013-07-04 37 views
3

相对布局的底部我对Android非常新。我正在创建一个包含可点击文本的应用程序。 所有可点击的文本都存在于一个数组中,然后该数组与ListView关联,并显示在RelativeLayout中。需要一个按钮在Android

代码是 -

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

主要是相对布局,标签布局中的文本框的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> 

结束输出为隐藏的实际内容下一个按钮的阵列。

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

回答

2

这是更简单。使用RelativeLayout做叫号。如果你想做到这一点使用LinearLayout,您可以设置的意见layout_height0dp和使用layout_weight分发高度

+0

你能检查一下代码并告诉我我在做什么错?我尝试了很多,但我得到的输出是不希望的。 – user2550128

2

我认为你应该做这样的:

<?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 希望这会有所帮助。

+0

我试过这个,但不知何故按钮显示为一个列表。下一个按钮的数量等于数组中元素的数量。根据需要在底部没有单个下一步按钮。数组中的实际文本不可见并且不可点击。可能每个Next按钮都躺在这些文本上并隐藏它们 – user2550128

+0

您可以编辑您的问题以显示xml文件吗? @ user2550128 –

+0

你的xml在我的模拟器中给出了正确的o/p。你在java文件中如何在scrollview中加载文本? @ user2550128 –

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" > 

    <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> 
+0

此代码和Rapunzel建议的代码基本相同。请看看创建的输出。它有点像按钮 按钮 按钮 按钮垂直排列,实际元素不可见 – user2550128

0

我做了Anamika的建议,它像一个魅力。

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

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

你上班了吗?

请让我知道如果你还在挣扎,我有看看它,并帮助你解决。