【发布时间】:2015-12-09 07:57:49
【问题描述】:
我想在ListView 下方动态添加两个Buttons 和一个textView。当我在模拟器中运行我的应用程序时,会显示两个 buttons 和 text,但是在 real device 中运行时它们没有显示。
在安卓工作室中
在模拟器中
提交Button重叠添加声明Button!
现在已经解决了重叠问题,但是即使有listView,按钮和文字仍然不显示。
最新代码如下:
under_list_view_button
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:layout_width="181dp"
android:layout_height="wrap_content"
android:id="@+id/addClaims1"
android:layout_marginLeft="15px"
android:drawableRight="@mipmap/claims"
android:text="Add Claims"/>
<Button
android:layout_width="130dp"
android:layout_height="wrap_content"
android:id="@+id/btnSave"
android:drawableRight="@mipmap/submit"
android:layout_marginLeft="450px"
android:text="Submit" />
</FrameLayout>
total_hours.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="175dp"
android:layout_height="33dp"
android:textColor="@color/indian_red"
android:textStyle="bold"
android:textSize="18sp"
android:id="@+id/textView10"
android:layout_x="174dp"
android:layout_y="16dp" />
</AbsoluteLayout>
正如我所说,两个button 和text 是在ListView 下方动态添加的,因此如果没有listView,它们将不会显示。
活动 A
footer = (AbsoluteLayout) getLayoutInflater().inflate(R.layout.total_hours, null);
totalHours = (TextView) footer.findViewById(R.id.textView10);
footerLayout = (FrameLayout) getLayoutInflater().inflate(R.layout.under_list_view_button, null);
btnSubmit = (Button) footerLayout.findViewById(R.id.btnSave);
btnAddClaims = (Button) footerLayout.findViewById(R.id.addClaims1);
objMyCustomBaseAdapter = new MyCustomBaseAdapter(getApplicationContext(), results, listview, footerLayout, footer);
listview.setAdapter(objMyCustomBaseAdapter);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { // receive from Activity B and populate ListView A
if (resultCode == RESULT_OK) {
if (requestCode == PROJECT_REQUEST_CODE) {
ReceiveProject = data.getStringExtra("Project");
ReceiveDescription = data.getStringExtra("Description");
ReceiveProgress = data.getIntExtra("progress", 0);
ReceiveTimeIn = data.getStringExtra("TimeIn");
ReceiveTimeOut = data.getStringExtra("TimeOut");
if (mClickedPosition == -1) { // if icon clicked
if (objMyCustomBaseAdapter != null)
objMyCustomBaseAdapter.addNewItem(ReceiveProject, ReceiveDescription, ReceiveProgress, ReceiveTimeIn, ReceiveTimeOut);
} else {
if (objMyCustomBaseAdapter != null)
objMyCustomBaseAdapter.changeItem(mClickedPosition, ReceiveProject, ReceiveDescription, ReceiveProgress, ReceiveTimeIn, ReceiveTimeOut);
}
MyCustomBaseAdapter
public class MyCustomBaseAdapter extends BaseAdapter{ // for ListView
private static ArrayList<SearchResults> searchArrayList;
FrameLayout footerLayout;
private LayoutInflater mInflater;
ListView listview;
AbsoluteLayout footer;
public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results,ListView listview,FrameLayout footerLayout,AbsoluteLayout footer) {
searchArrayList = results;
this.listview=listview;
this.footerLayout=footerLayout;
mInflater = LayoutInflater.from(context);
this.footer=footer;
addOrRemoveFooter();
}
public void addOrRemoveFooter(){
if(searchArrayList.size() == 0 && listview.getFooterViewsCount() > 0){
listview.removeFooterView(footer);
listview.removeFooterView(footerLayout);
}else if(listview.getFooterViewsCount() == 0 && searchArrayList.size() > 0){
listview.addFooterView(footer);
listview.addFooterView(footerLayout);
}
}
public void changeItem(int m,String P,String D,int Per,String TI,String TO)
{
SearchResults obj = new SearchResults();
obj.setProject(P);
obj.setDescription(" Work Description : " + D);
obj.setProgress(" Progress : " + Per);
obj.setTimeIn(" Time In : " + TI);
obj.setTimeOut(" Time Out : " + TO);
searchArrayList.set(m,obj);
this. notifyDataSetChanged();
}
public void addNewItem(String P,String D,int Per,String I,String O)
{
SearchResults obj = new SearchResults();
obj.setProject(P);
obj.setProgress(" Progress : " + Per);
obj.setTimeIn(" Time In : " + I);
obj.setTimeOut(" Time Out : " + O);
obj.setDescription(" Work Description : " + D);
searchArrayList.add(obj);
this. notifyDataSetChanged();
addOrRemoveFooter();
}
}
我使用华为运行该应用程序,它不显示文本和按钮。但是它们在三星手机上运行时会显示...
【问题讨论】:
-
我的意思是你的java代码
-
您应该使用不同的 ViewGroup - 例如,LinearLayout 或 RelativeLayout - 因为“FrameLayout 旨在阻止屏幕上的区域以显示单个项目。” Docs
-
@jas 张贴..请检查
-
@MikeM。所以我应该使用 RelativeLayout 而不是 Frame ?
-
您发布的代码不一致。您的 Adapter 实例化使用了一个五参数构造函数,但您只在 Adapter 类中显示了一个四参数构造函数。