灵活的相对布局与列表和图像按钮

时间:2011-02-22 12:32:34

标签: android android-layout relativelayout

我想在android中创建以下类型的布局,现在我用listview的固定高度创建了rite,这导致了不同尺寸的手机屏幕的问题。

最初当出现此屏幕时,按钮将位于底部,只有空列表视图,并且当项目将在列表视图中添加时,它会增长,但按钮将保持稳定在底部。

到目前为止,我已经编写了以下代码

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


   <ImageView
    android:id="@+id/cText" 
    android:src="@drawable/c_text"
    android:visibility="visible"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="50dip"



   />
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"    
        android:drawSelectorOnTop="false"
        />
<ImageButton
        android:id="@+id/cBtn"
        android:layout_width="150dip"
        android:layout_height="52dip"
        android:src="@drawable/preview" 
        android:layout_alignParentTop="@+id/list"
        android:layout_gravity="center_horizontal"/>
</RelativeLayout>

2 个答案:

答案 0 :(得分:2)

试试这个

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

 <ImageButton
    android:id="@+id/cBtn"
    android:layout_centerHorizontal="true"
    android:layout_width="150dip"
    android:layout_height="52dip"
    android:src="@drawable/preview" 
    android:layout_alignParentBottom="true"
    />

<ListView
    android:id="@+id//listview01"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"    
    android:layout_above="@+id/cBtn"
    android:layout_marginBottom="10dip"
            />

< /RelativeLayout>

答案 1 :(得分:0)

您可以将Button作为页脚添加到列表视图。示例代码sinppet如下所示

第1步:创建一个按钮

  <Button android:id="@+id/footer"  android:gravity="center"
            android:layout_gravity="center"
            android:text="My Footer" 
            android:layout_width="wrap_content" 
            android:layout_height="0dp" android:layout_weight="1"/> 

步骤2:将其作为列表视图的页脚

ListView myList;
  View footerView;
    ViewGroup footerViewOriginal = (ViewGroup) findViewById(R.id.footer);
        footerView = inflater2.inflate(R.layout.footer,footerViewOriginal);
footerView.findViewById(R.id.footer);
myList.addFooterView(footerView, null, true);

步骤3:在ClickListener上创建并编写您要执行的操作

footerView.findViewById(R.id.footer).setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
return false;

        }