Как запрограммировать список кнопок с 3 различными текстами в каждом - PullRequest
0 голосов
/ 01 июня 2019

Я пытаюсь сделать код, чтобы сделать кнопки с 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 голосов
/ 02 июня 2019

Это один из многих способов сделать это:

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

Вам придется изменить его в соответствии с вашими потребностями.

Удачи, и дайте мне знать, если вам нужна помощь.

...