片段与活动

时间:2017-03-18 16:11:03

标签: android xml android-layout android-fragments

我做了progress-bar fragment;目标是在点击MainActivity中的按钮时显示它。但是,我遇到了这种困境(使用Android 5.1):

https://gyazo.com/8632f6e50cdce599847258939cc4f109

有什么想法,这里的交易是什么?我不能指出问题所在。唯一的解决方案是在单击时隐藏按钮。它有效,但我解释的是代码编写错误。

主要活动

@Override
protected void onCreate(Bundle savedInstanceState) {
    final Button bRegister = (Button) findViewById(R.id.bRegister);
    final Button bTest = (Button) findViewById(R.id.bTest);
    bTest.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.activity_register, progressFragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });

ProressFragment

public class ProgressFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_progress, container, false);
        ProgressBar pb = (ProgressBar) view.findViewById(R.id.progressBar);
        pb.setVisibility(View.VISIBLE);
        return view;
    }
}

适用于MainActivity的XML (看起来像这样:https://gyazo.com/8ecef7ada74cf92730b4d99757383da7

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_register"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="googleplayservices.samples.android.teamtreehouse.com.kibbleuserc.RegisterActivity">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text="Password"
        android:ems="10"
        android:id="@+id/etPassword"
        android:layout_below="@+id/etEmail"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignRight="@+id/etEmail"
        android:layout_alignEnd="@+id/etEmail"/>

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text="Phone Number"
        android:ems="10"
        android:id="@+id/etPhoneNum"
        android:layout_below="@+id/etPassword"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignRight="@+id/etPassword"
        android:layout_alignEnd="@+id/etPassword"/>

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text="Name"
        android:ems="10"
        android:id="@+id/etName"
        android:layout_below="@+id/etPhoneNum"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignRight="@+id/etPhoneNum"
        android:layout_alignEnd="@+id/etPhoneNum"/>

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text="Email"
        android:ems="10"
        android:layout_marginTop="10dp"
        android:id="@+id/etEmail"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/etEmail"
        android:id="@+id/tvEmailError"
        android:layout_above="@+id/etPassword"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignTop="@+id/etEmail"
        android:textColor="#FFFF4444"/>

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tvPasswordError"
        android:layout_alignBottom="@+id/etPassword"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_toRightOf="@+id/etPassword"
        android:layout_toEndOf="@+id/etPassword"
        android:layout_below="@+id/etEmail"
        android:textColor="#FFFF4444"/>

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/etPhoneNum"
        android:layout_toRightOf="@+id/etPhoneNum"
        android:id="@+id/tvPhoneNumError"
        android:layout_below="@+id/etPassword"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:textColor="#FFFF4444"/>

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/etName"
        android:layout_toRightOf="@+id/etName"
        android:id="@+id/tvNameError"
        android:layout_below="@+id/etPhoneNum"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:textColor="#FFFF4444"/>

    <Button
        android:text="Register"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/bRegister"
        android:layout_below="@+id/etName"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="86dp"/>

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/bRegister"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="31dp"
        android:id="@+id/bTest"/>


</RelativeLayout>

XML for ProgressFragment(看起来像这样:https://gyazo.com/93bbb11af8cb8a0df8d4d6cf97b84ef1

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
    android:background="@android:color/holo_green_light">

    <ProgressBar
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

之所以发生这种情况,是因为当 $(document).ready(function () { $.post('geterpitem', { grn: $('#num').val() }, function (responseJson) { if (responseJson.length != null) { defineTable(responseJson); } else { $('#itemtable').DataTable(); alert("No item found!!"); } }); }); function defineTable(jsonData){ var lineNum = 1; $('#itemtable').DataTable({ data: jsonData, columnDefs:[{ targets: [4,5,6,7], render: function () { return "<input type='text' />";} }], columns: [ { data: function () {return lineNum++;}}, { data: "itemcode" }, { data: "itemname" }, { data: "receivedqty" }, { data: null }, { data: null, className:"aNumber" }, { data: null }, { data: null }, ] }); $('#itemtable .aNumber').on("keypress", "input", function (evt) { isNumber(evt);}) } RelativeLayout的海拔高度为0时,这两个按钮的高程不为0。

您可以修复Fragment RelativeLayout Activity的{​​{1}},如下所示:

FrameLayout

但是,优化后的解决方案是使用<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_register" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="googleplayservices.samples.android.teamtreehouse.com.kibbleuserc.RegisterActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!-- Add all the views that you have inserted in your RelativeLayout --> </RelativeLayout> </FrameLayout> 设置FragmentView的高程,但您必须知道要设置的正确高程{{1}在其他人之上。在这种情况下,您将不需要ViewCompat.setElevation(View,int)包装,并且ViewFrameLayout调用将得到优化。