android - 更改 Android 视图中的项目顺序
问题描述
我有两个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>
推荐阅读
- javascript - 使用 JavaScript 进行 JSON 转换
- python - 如何在 CPython 中将错误消息作为字符串获取
- mongodb - 将 MongoDB 从 2.6 升级到 4.2.6
- corda - 如何在 TokenSDK 中使用 addIssueTokens Utility 方法
- ruby-on-rails - Money-rails 显示金额为数千
- c++ - 当我在 Ubuntu 终端上发送 SIGTERM (Ctrl + C) 时,为什么我的 C++ 代码没有终止?
- javascript - 在 keyup 函数中使用通配符
- nginx - 获取 Web 服务的 kerberos/Windows AD 登录工作的问题
- google-apps-script - 将特定数据从工作表复制到工作表
- javascript - 如何在 Node.js 中使用 Jimp 调整图像大小以进行上传而不刷新