如何对按钮列表进行编程,每个按钮中包含3种不同的文本

时间:2019-05-31 21:59:29

标签: android-studio-3.0

我正在尝试编写代码,使每个按钮中包含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


1 个答案:

答案 0 :(得分:0)

这是多种方法之一:

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);
        }

    }

屏幕截图 screenshot

您必须根据需要进行更改。

祝你好运,如果需要帮助,请告诉我。