【发布时间】:2020-01-23 06:21:23
【问题描述】:
我正在尝试在两列之间画线,每列包含 10 个按钮,如下图所示。我尝试使用线性布局。
private void createCHTDesign(int scenarios, int numberOfPlots, ArrayList<Plots> plotsArrayList) {
headingLayout.clear();
Log.d(TAG, "scenarios:" + scenarios);
Log.d(TAG, "headingsize:" + headingLayout.size());
for (int i = 0; i < scenarios; i++) {
LinearLayout linearLayout = new LinearLayout(mContext);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
if (i == 0) {
params.setMargins(0, 0, 0, 0);
} else {
params.setMargins(250, 0, 0, 0);
}
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(params);
mainLayout.addView(linearLayout);
headingLayout.add(linearLayout);
Log.d(TAG, "count:" + mainLayout.getChildCount());
}
for (int i = 0; i < headingLayout.size(); i++) {
addButtons(headingLayout.get(i), i, numberOfPlots, plotsArrayList);
}
}
@SuppressLint("ClickableViewAccessibility")
private void addButtons(LinearLayout layout, int index, int numberOfPlots, ArrayList<Plots> plotsArrayList) {
TextView textView = new TextView(mContext);
LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
textView.setLayoutParams(tvParams);
textView.setText(combinationNames.get(index));
textView.setTextSize(16);
layout.addView(textView);
for (int i = 0; i < numberOfPlots; i++) {
Button button = new Button(mContext);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(400, 150);
button.setLayoutParams(params);
button.setId(View.generateViewId());
button.setText(plotsArrayList.get(i).getMpValue() + "\n" + plotsArrayList.get(i).getYield());
button.setGravity(Gravity.CENTER_VERTICAL);
button.setPadding(15, 0, 0, 0);
button.setTextColor(Color.parseColor("#FFFFFF"));
button.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.right2x, 0);
GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setColor(Color.parseColor(colors[i]));
gradientDrawable.setCornerRadius(10);
gradientDrawable.setStroke(20, Color.parseColor(colors[i]));
clickonArrow(button.getText().toString(), button);
button.setBackground(gradientDrawable);
layout.addView(button);
}
}
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"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1"
android:layout_margin="10dp">
<androidx.appcompat.widget.AppCompatSpinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_weight="0.3"
android:layout_height="wrap_content"
android:spinnerMode="dropdown"
android:gravity="center"
style="@style/SpinnerTheme"
/>
<androidx.appcompat.widget.AppCompatSpinner
android:id="@+id/spinner2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:spinnerMode="dropdown"
android:gravity="center"
style="@style/SpinnerTheme"/>
<androidx.appcompat.widget.AppCompatSpinner
android:id="@+id/spinner3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:gravity="center"
android:spinnerMode="dropdown"
style="@style/SpinnerTheme" />
</LinearLayout>
<EditText
android:id="@+id/searchCht"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_search"
android:layout_marginLeft="10dp"
android:hint="Search CHT"
android:backgroundTint="@android:color/black"
android:layout_marginRight="10dp"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:fillViewport="true"
android:scrollbars="horizontal">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- <com.sma.cht.chtcharacterization.view.MatchTheFollowingAttempted
android:id="@+id/match"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbarThumbHorizontal="@color/colorPrimary"
android:scrollbars="horizontal">
</com.sma.cht.chtcharacterization.view.MatchTheFollowingAttempted>
-->
</LinearLayout>
</HorizontalScrollView>
</ScrollView>
</LinearLayout>
目前,我在两列中都获得了按钮,但无法画线,您也可以使用 listview 来完成这些按钮的工作。你能帮我解决这个问题吗?提前致谢。
【问题讨论】:
标签: android canvas bitmap android-linearlayout