Android 按视图和布局调用另一个XML
我想在我的活动中调用另一个XML的每个主体,我都调用了我的主XML,如下所示Android 按视图和布局调用另一个XML,android,view,layout-inflater,Android,View,Layout Inflater,我想在我的活动中调用另一个XML的每个主体,我都调用了我的主XML,如下所示 setContentView(R.layout.main); 我的xml文件如下- <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content"
setContentView(R.layout.main);
我的xml文件如下-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/appbg"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/ll2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<!-- listview -->
<ImageView
android:id="@+id/img2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:contentDescription="@drawable/logo12"
android:src="@drawable/logo12" />
<LinearLayout
android:id="@+id/homeLin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="right" >
<Button
android:id="@+id/btnCart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:background="@drawable/cart_icon" />
<Button
android:id="@+id/btnHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:background="@drawable/home" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="@+id/searchLin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/etSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/search"
android:textColor="@color/Black"
android:textSize="15sp" >
<requestFocus />
</EditText>
<ImageView
android:id="@+id/cross1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:layout_marginRight="5dp"
android:contentDescription="@drawable/search"
android:src="@drawable/search" />
</FrameLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/linSortby"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/tvsortby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sortby"
android:textColor="@color/Blue"
android:textSize="13sp" />
<TextView
android:id="@+id/tvPriceLH"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/priceLH"
android:textColor="@color/Blue"
android:textSize="13sp" />
<TextView
android:id="@+id/tvPriceHL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:text="@string/priceHL"
android:textColor="@color/Blue"
android:textSize="13sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/linPagingupper"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:layout_marginTop="5dp"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/linPaging"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imgPrev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="2dp"
android:contentDescription="@drawable/prev"
android:src="@drawable/prev" />
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@drawable/buttonbackground"
android:text="@string/n1"
android:textColor="@color/Blue"
android:textSize="12sp" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:background="@drawable/buttonbackground"
android:text="@string/n2"
android:textColor="@color/Blue"
android:textSize="12sp" />
<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:background="@drawable/buttonbackground"
android:text="@string/n3"
android:textColor="@color/Blue"
android:textSize="12sp" />
<TextView
android:id="@+id/tv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:background="@drawable/buttonbackground"
android:text="@string/n4"
android:textColor="@color/Blue"
android:textSize="12sp" />
<TextView
android:id="@+id/tv5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:background="@drawable/buttonbackground"
android:text="@string/n5"
android:textColor="@color/Blue"
android:textSize="12sp" />
<!-- button -->
<ImageView
android:id="@+id/imgNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="2dp"
android:contentDescription="@drawable/next"
android:src="@drawable/next" />
</LinearLayout>
<TextView
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/n1"
android:textColor="@color/white"
android:textSize="12sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/listViewResult"
android:layout_width="fill_parent"
android:layout_height="320dp"
android:layout_marginTop="10dp"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#b5b5b5"
android:dividerHeight="1dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/linBottom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/bluegrad"
android:gravity="bottom"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imgContact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:contentDescription="@drawable/contact"
android:src="@drawable/contact" />
<LinearLayout
android:id="@+id/bottomLin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right" >
<ImageView
android:id="@+id/imgHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:contentDescription="@drawable/home"
android:src="@drawable/home" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/add_other_xml"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
我的pay_now_btn.xml如下
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/btn_paynow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp" />
问题是我做每一件事的方式都是正确的。那么为什么我的文本视图没有出现。无错误无例外,我无法查看pay_now_btn.xml的文本视图
提前感谢大家。您尚未将视图添加到
main.xml中的布局中
在main.xml中创建布局
在代码中
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
layout.addView(pay_now_view);
希望这能奏效。如果你遇到任何问题,请告诉我
代码:
在关闭最后一个标记之前,将其添加到main.xml的底部
<LinearLayout
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp" >
</LinearLayout>
您有setContentView(R.layout.main)
你在夸大一个观点。但膨胀视图不会添加到布局中
在main.xml
中创建另一个LinearLayout
。将此线性布局放置到所需位置,也可以使用Relativelayout
在onCreate中初始化它
LinearLayout ll = LinearLayout findViewById(R.id.ll);
现在将充气视图添加到linearlayout
ll.addView(pay_now_view);
编辑:
您可以将嵌入视图作为页脚添加到listview,如下所示
list.addFooterView(pay_now_view );
您必须将内容设置为活动。您已将main.xml设置为main.xml中的布局线性或相对布局,并将视图添加为linearlayout或相对布局。您必须在main.xml中的任何布局中添加pay_now_视图。感谢您在32秒内的回复。我没有得到您想要说的内容…main.xml是我的父布局。所以我叫它。问题不在于主要问题。xml@selva谢谢。。你能再发一些代码吗?\@BlueGreen我把它作为一个明确的答案发了出来。我试过这个,但它对我不起作用。我真的受够了这样一个愚蠢的问题。@BlueGreen我看不出有什么理由它不起作用。通过编辑question@BlueGreen您有这个linearlayoutlain\u lay\u add\u paynow=
应该是linearlayoutlain\u lay\u add\u paynow=LinearLayout findViewById(R.id.add\u other\u xml)
@BlueGreen使用线性布局的id初始化linearlayout。@BlueGreen您对linearlayout的初始化是错误的,应该是linearlayout-lin\u-lay\u-add\u-paynow=linearlayout-findViewById(R.id.add\u-other\u-xml)
。请更新它。我从上到下都看过你的代码。
ll.addView(pay_now_view);
list.addFooterView(pay_now_view );