簡體   English   中英

Android視圖中的項目更改順序

[英]Change order of items in Android View

我有兩個xml。 device_list_header.xml 和 device_list_item.xml。 device_list_header.xml 有兩個項目,TextView 和 Button。 device_list_item.xml 有兩個 TextView。

device_list_header.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?android:attr/listDivider"
    android:orientation="vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="12dp"
        android:layout_marginBottom="12dp"
        android:height="30dp"
        android:gravity="center"
        android:text="@string/devices"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium" />    
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="80dp"
        android:layout_marginBottom="20dp"
        android:background="@android:color/holo_green_light"
        android:text="Launch Gesture Recognition"
        android:textColor="@android:color/white" />
</LinearLayout>

device_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/text1"
        android:layout_marginTop="12dp"
        android:layout_marginStart="12dp"
        android:layout_marginEnd="12dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
    <TextView
        android:id="@+id/text2"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        android:layout_marginBottom="12dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="@style/TextAppearance.AppCompat.Small" />
</LinearLayout>

當視圖膨脹時,按鈕位於列表項的頂部。

如何更改以便首先列出項目? 然后按鈕在底部。 在此處輸入圖像描述

我的代碼在這里

public class DevicesFragment extends ListFragment {
    private ArrayList<ListItem> listItems = new ArrayList<>();
    private ArrayAdapter<ListItem> listAdapter;
    private int baudRate = 19200;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setHasOptionsMenu(true);
        listAdapter = new ArrayAdapter<ListItem>(getActivity(), 0, listItems) {
            @Override
            public View getView(int position, View view, ViewGroup parent) {
                ListItem item = listItems.get(position);
                if (view == null)
                    view = getActivity().getLayoutInflater().inflate(R.layout.device_list_item, parent, false);
                TextView text1 = view.findViewById(R.id.text1);
                TextView text2 = view.findViewById(R.id.text2);

                if(item.driver == null)
                    text1.setText("<no driver>");
                else if(item.driver.getPorts().size() == 1)
                    text1.setText(item.driver.getClass().getSimpleName().replace("SerialDriver",""));
                else
                    text1.setText(item.driver.getClass().getSimpleName().replace("SerialDriver","")+", Port "+item.port);
                text2.setText(String.format(Locale.US, "Vendor %04X, Product %04X", item.device.getVendorId(), item.device.getProductId()));
                return view;
            }
        };

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setListAdapter(null);
        View header = getActivity().getLayoutInflater().inflate(R.layout.device_list_header, null, false);
        getListView().addHeaderView(header, null, false);
        setEmptyText("<no USB devices found>");
        ((TextView) getListView().getEmptyView()).setTextSize(18);
        setListAdapter(listAdapter);
        final Button switchToGesture = (Button) header.findViewById(R.id.button);
        switchToGesture.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                switchToGesture.setBackgroundColor(Color.BLUE);
            }
        });
    }
}

我是這樣修改的。 制作另一個 xml 並放在下面。

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setListAdapter(null);
        View header = getActivity().getLayoutInflater().inflate(R.layout.device_list_header, null, false);
        getListView().addHeaderView(header, null, false);
        setEmptyText("<no USB devices found>");
        ((TextView) getListView().getEmptyView()).setTextSize(18);
        setListAdapter(listAdapter);
        View buttonview = getActivity().getLayoutInflater().inflate(R.layout.button_layout, null, false);
        final Button switchToGesture = (Button) buttonview.findViewById(R.id.button);
        switchToGesture.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                switchToGesture.setBackgroundColor(Color.BLUE);
            }
        });
        getListView().addFooterView(buttonview, null, false);
    }

button_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="80dp"
        android:layout_marginBottom="20dp"
        android:background="@android:color/holo_green_light"
        android:text="Launch Gesture Recognition"
        android:textColor="@android:color/white" />
</LinearLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM