android片段xml奇怪的问题

时间:2016-06-30 04:18:12

标签: java android xml layout fragment

当我添加LinearLayout ListView且片段为单线性时, 奇怪的是android studio制作单线性双线性。

我的问题就在下面。看白色文字 enter image description here

这是主要布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="@drawable/_ff_ff_ff"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/reg_step_layout"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:layout_weight="1"
         android:orientation="vertical">

    <fragment android:id="@+id/mainStage"
        android:name="com.xxx.StageOne"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout="@layout/stageone" />

    </LinearLayout>
</LinearLayout>
下面的

是要添加到片段

的子布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/reg_hide_area1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/reg_found_wifi"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="visible"
        android:background="#123456"
        android:orientation="vertical"
        android:weightSum="1">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

        <ListView
            android:id="@+id/reg_wifi_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:choiceMode="singleChoice"
            android:divider="@null"
            android:listSelector="@drawable/_ed_ed_ed">
            android:background="#ffffff">

        </ListView>
        </LinearLayout>
        <LinearLayout                    // this is weird code 
            android:id="@+id/reg_progress"
            android:layout_width="match_parent"
            android:layout_height="@dimen/_140"
            android:background="#883347"
            android:orientation="horizontal"
            android:layout_weight="1">

        </LinearLayout>
    </LinearLayout>
</LinearLayout>

下面是片段java代码。

 public class StageOne extends Fragment {
public ImageView ctrlRefresh;
public ImageView ctrlLoading;
public AnimationDrawable spinLoding;
public LinearLayout noWifiLayout, wifiLayout;
public ListView wifiListView;
public RegisterListViewAdapter adapter;
public ArrayList<ListRow> lists = new ArrayList<ListRow>();

private OnFragmentInteractionListener mListener;

public interface OnFragmentInteractionListener {
    void setListener(ListView listView, ImageView ctrlRefresh);
}

public StageOne() {
    // Required empty public constructor
}

public static StageOne newInstance() {
    StageOne instance = new StageOne();
    return instance;
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    }
}

@SuppressWarnings("deprecation")
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        if (activity instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) activity;
        }
    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = null;
    view = inflater.inflate(R.layout.stageone, container, false);

    wifiLayout = (LinearLayout) view.findViewById(R.id.reg_found_wifi);
    wifiLayout.setVisibility(View.VISIBLE);

    wifiListView = (ListView) view.findViewById(R.id.reg_wifi_list);
    adapter = new RegisterListViewAdapter(
            getActivity(),
            R.layout.activity_register_listview_row,
            lists);
    wifiListView.setAdapter(adapter);

    /* view.findViewById(R.id.reg_btn_network_refresh_loading);
     * view.findViewById(R.id.reg_btn_network_refresh);
     * if (mListener != null && ctrlRefresh != null) {
     *    mListener.setListener(wifiListView, ctrlRefresh);
     * }
     */
    return view;
}
}
下面的

是片段初始化代码

 void updateStage() {

    layparam = (LinearLayout.LayoutParams) midSpaceLayout.getLayoutParams();
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();

    switch (stage) {
        case 1:
            layparam.height = (int)getResources().getDimension(R.dimen._255);

            ivStage1.setImageResource(R.mipmap.img_step01_ing);
            ivStage2.setImageResource(R.mipmap.img_step02_n);
            ivStage3.setImageResource(R.mipmap.img_step03_n);
            ivStage4.setImageResource(R.mipmap.img_step04_n);
            ctrlMidTxt.setText(getResources().getString(R.string.stageMsg1));

            refreshAdapter();

            stageOne = StageOne.newInstance();        //this is fragment init
            ft.replace(R.id.mainStage, stageOne);
            ft.commit();

            btnPrev.setVisibility(View.VISIBLE);
            btnPrev.setEnabled(false);
            btnNext.setEnabled(false);
            break;

        case 2:
            stage1Pressed = 0;
            stage1Connected = 0;
            checkConnect = false;
            stageOne.lists.clear();

            layparam.height = (int) etResources().getDimension(R.dimen._155);
            ivStage1.setImageResource(R.mipmap.img_step01_complete);
            ivStage2.setImageResource(R.mipmap.img_step02_ing);
            ivStage3.setImageResource(R.mipmap.img_step03_n);
            ivStage4.setImageResource(R.mipmap.img_step04_n);
            ctrlMidTxt.setText(getResources().getString(R.string.stageMsg2));

            stageTwo = StageTwo.newInstance();   // this is to be repolaced
            ft = fm.beginTransaction();          // not yet impl
            ft.replace(R.id.mainStage, stageTwo);
            ft.commit();

            btnPrev.setVisibility(View.VISIBLE);
            btnPrev.setEnabled(true);
            break;
    }
}

}

提前谢谢

0 个答案:

没有答案