按钮显示在模拟器中,但不在真实设备中

时间:2015-12-09 07:57:50

标签: android xml button layout

我想动态地在Buttons下添加两个textView和一个ListView。当我在模拟器中运行我的应用时,会显示两个buttonstext,但在real device中运行时,它们没有显示

在android studio

enter image description here

在模拟器中

enter image description here

提交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>

正如我所说的那样,{strong} buttontext动态添加到 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();

    }
}

我使用华为来运行应用程序,它不显示文本和按钮。但他们在三星手机上运行时显示......

2 个答案:

答案 0 :(得分:1)

如果您不想重叠按钮:

  1. btnSave
  2. 正确上提供addClaims1
  3. marginLeft降低到15px左右(如addClaims1按钮)

答案 1 :(得分:0)

使用此代码:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center" >

        <Button
            android:id="@+id/addClaims1"
            android:layout_width="181dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15px"
            android:text="Add Claims" />

        <Button
            android:id="@+id/btnSave"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5px"
            android:layout_toRightOf="@+id/addClaims1"
            android:text="Submit" />

    </RelativeLayout>

</FrameLayout>