如何在我的主布局中显示自定义布局?

时间:2014-03-11 08:08:33

标签: android

我在我的主要布局中使用自定义列表视图https://github.com/NikolaDespotoski/DoubleTapListViewHandler/tree/master/src/com/nikola/despotoski/doubletaplistview但问题是这将创建自定义programicaly布局但在我的代码中我已经设置了内容setContentView(R.layout.bid);那么如何在我的主布局中添加新的自定义布局?帮帮我怎么办?

 private CustomListView bidlist = null;


public class BidSchema extends BackBaseActivity  implements OnItemDoubleTapLister {

     @Override
     protected void onCreate(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          super.onCreate(savedInstanceState);
          setContentView(R.layout.bid);

          bidlist = new CustomListView(this);
          bidlist.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
          setContentView(bidlist);
          back = (ImageView) findViewById(R.id.bidback);
          bottomlayout = (RelativeLayout)findViewById(R.id.bottom_layout);
          scroll_down = (ImageView) findViewById(R.id.down);
          scroll_up = (ImageView) findViewById(R.id.up);

布局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="wrap_content"
    android:background="@drawable/bg2" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/top_bar1"
        android:contentDescription="@string/contentdesc" />

    <ImageView
        android:id="@+id/bidback"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="6dp"
        android:layout_marginLeft="3dp"
        android:background="@drawable/back_arr"
        android:contentDescription="@string/contentdesc" />

     <ImageView
        android:id="@+id/iconpic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="4dp"
        android:layout_centerHorizontal="true"
        android:background="@drawable/treasure"
        android:contentDescription="@string/contentdesc" />


    <TextView
        android:id="@+id/bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/imageView1"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:background="@drawable/bar"
        android:contentDescription="@string/contentdesc"
        android:gravity="center"
        android:text="Bid"
        android:textColor="#000000"
        android:textSize="22sp" />

     <LinearLayout
        android:id="@+id/jobhead"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/bar"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10sp"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/jobDate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingLeft="20dp"
            android:textColor="#ffffff"
            android:textSize="22sp"


            android:gravity="left"
            android:text="Plot"

            android:textStyle="bold" />

        <TextView
            android:id="@+id/jobpick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:gravity="right"
            android:paddingRight="50dp"
            android:text="Job"
            android:textColor="#ffffff"
            android:textSize="22sp"
            android:textStyle="bold" />
         </LinearLayout>

    <ListView
        android:id="@+id/bidlist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/jobhead"
        android:layout_marginBottom="10dp"
        android:layout_above="@+id/bottomBar"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/fields_bg"
        android:visibility="visible" >
       </ListView>

    <include
        android:id="@+id/bottomBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        layout="@layout/bottom" />

</RelativeLayout>

3 个答案:

答案 0 :(得分:1)

如果您已实现自定义Listview,则应更改此内容。

      setContentView(R.layout.bid);

      bidlist = new CustomListView(this);
      bidlist.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
      setContentView(bidlist);

      bidlist = new CustomListView(this);
      bidlist.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
      setContentView(bidlist);

更新:

RelativeLayout rootLayout = (RelativeLayout) findViewById(R.id.layout);

bidlist = new CustomListView(this);

RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

rootLayout.addView(bidlist, rlp);

因为你不能两次调用setContentView()。因此,您必须将其添加到父相对布局中。所以检查一下。

答案 1 :(得分:0)

你没有setContentView 如果主要活动显示已发布xml,只需使用Adapter实现baseAdapter,然后只使用listview.setAdapter(你制作适配器)

答案 2 :(得分:0)

如下所示向您的父id提供RelativeLayout,以便稍后向此布局添加新视图...

<?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="wrap_content"
    android:background="@drawable/bg2"
    android:id="@+id/layout" >

    ..........

</RelativeLayout >

现在,将您的bidlist添加到父布局,如下所示......

RelativeLayout rootLayout = (RelativeLayout) findViewById(R.id.layout);

bidlist = new CustomListView(this);

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

rootLayout.addView(bidlist, lp);