我试图根据设备大小多次充气单行,这里是代码: -
private void get_device_size() {
DisplayMetrics displayMetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
for (int i = 0; i < width / 20; i++) {
Log.e("i", String.valueOf(i));
setup_scroller(i, width / 20);
}
}
private void setup_scroller(int i, int total) {
LayoutInflater inflater;
inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.layout_line_scroller_single_row, null);
LinearLayout linear_line = (LinearLayout) v.findViewById(R.id.linear_line);
if (i == total / 2) {
linear_line.setBackgroundColor(getResources().getColor(R.color.colorAccent));
} else if (i == (total - 2) / 2) {
linear_line.setBackgroundColor(getResources().getColor(R.color.base_color_green));
} else if (i == (total - 3) / 2) {
linear_line.setBackgroundColor(getResources().getColor(R.color.base_color_green));
} else if (i == (total + 2) / 2) {
linear_line.setBackgroundColor(getResources().getColor(R.color.base_color_green));
} else if (i == (total + 4) / 2) {
linear_line.setBackgroundColor(getResources().getColor(R.color.base_color_green));
} else {
linear_line.setBackgroundColor(getResources().getColor(R.color.base_color_green_alpha));
}
linear_line_Scroller.addView(v);
}
layout_line_scroller_single_row: -
<?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:gravity="center"
android:orientation="vertical">
<LinearLayout
android:id="@+id/linear_line"
android:layout_width="2dp"
android:layout_height="15dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:orientation="horizontal"
android:background="@color/base_color_green"></LinearLayout>
</LinearLayout>
我正在从此代码获得以下输出。 我希望所有屏幕尺寸设备都在屏幕中央突出显示的行。
谢谢!