如何在每个按钮列表中包含 3 个不同的文本
How to program button list with 3 different texts in each
我正在尝试编写代码来制作按钮,每个按钮包含 3 种不同的文本。
像这样:
Text|Text
Text|
_________
Text|Text
Text|
_________
Text|Text
Text|
https://imgshare.io/image/wHXHd
The vertical lines are joined which means there are three sections to a button. How should I go about doing this? Any help would be appreciated
这是您可以实现的众多方法之一:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
</LinearLayout>
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout ll = (LinearLayout)findViewById(R.id.main_layout);
for (int i = 0;i<3;i++) {
LinearLayout ll1 = new LinearLayout(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ll1.setOrientation(LinearLayout.HORIZONTAL);
ll.addView(ll1, lp);
LinearLayout ll2 = new LinearLayout(this);
lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ll2.setOrientation(LinearLayout.VERTICAL);
ll1.addView(ll2, lp);
Button btn1 = new Button(this);
btn1.setText("1");
ll2.addView(btn1, lp);
Button btn2 = new Button(this);
btn2.setText("2");
ll2.addView(btn2, lp);
Button btn3 = new Button(this);
btn3.setText("3");
lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
ll1.addView(btn3, lp);
}
}
截图
您必须对其进行更改以满足您的需要。
祝你好运,如果你需要帮助,请告诉我。
我正在尝试编写代码来制作按钮,每个按钮包含 3 种不同的文本。
像这样:
Text|Text
Text|
_________
Text|Text
Text|
_________
Text|Text
Text|
https://imgshare.io/image/wHXHd
The vertical lines are joined which means there are three sections to a button. How should I go about doing this? Any help would be appreciated
这是您可以实现的众多方法之一:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
</LinearLayout>
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout ll = (LinearLayout)findViewById(R.id.main_layout);
for (int i = 0;i<3;i++) {
LinearLayout ll1 = new LinearLayout(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ll1.setOrientation(LinearLayout.HORIZONTAL);
ll.addView(ll1, lp);
LinearLayout ll2 = new LinearLayout(this);
lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ll2.setOrientation(LinearLayout.VERTICAL);
ll1.addView(ll2, lp);
Button btn1 = new Button(this);
btn1.setText("1");
ll2.addView(btn1, lp);
Button btn2 = new Button(this);
btn2.setText("2");
ll2.addView(btn2, lp);
Button btn3 = new Button(this);
btn3.setText("3");
lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
ll1.addView(btn3, lp);
}
}
截图
您必须对其进行更改以满足您的需要。
祝你好运,如果你需要帮助,请告诉我。