Android 如何在我的主布局中显示自定义布局?
从这里开始,我在我的主布局中使用自定义listview,但问题是这将创建自定义编程布局,但在我的代码中,我已经设置了content setContentView(R.layout.bid);那么如何在我的主布局中添加新的自定义布局??帮帮我我该怎么办Android 如何在我的主布局中显示自定义布局?,android,Android,从这里开始,我在我的主布局中使用自定义listview,但问题是这将创建自定义编程布局,但在我的代码中,我已经设置了content setContentView(R.layout.bid);那么如何在我的主布局中添加新的自定义布局??帮帮我我该怎么办 private CustomListView bidlist = null; public class BidSchema extends BackBaseActivity implements OnItemDoubleTapLister
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>
您不需要设置ContentView。
如果主活动显示发布的xml,只需使适配器实现baseAdapter,然后只需listview.setAdapter(您创建适配器)如果您实现了自定义listview,则应更改此设置
setContentView(R.layout.bid);
bidlist = new CustomListView(this);
bidlist.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
setContentView(bidlist);
到
更新:
因为不能两次调用setContentView()。因此,您必须将其添加到父相对布局中。因此,请检查。向您的家长
RelativeLayout
提供一个id
,如下所示,以便稍后在此布局中添加新视图
<?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 >
发布你的布局xml。检查它我发布了你的代码你做了两次设置contentView,这可能是你的问题的障碍。第一次setContentView(R.layout.bid);第二次使用setContentView(bidlist);适配器实现baseAdapter它的意思是什么?我从这个类扩展了OnItemDoubletaLister,所以我将如何在我的主布局中显示listviewme@user3388003谢谢我的回答。
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);
<?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 >
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);