java - 我无法在表格 View 内设置按钮宽度

标签 java android android-layout

我正在做一个android应用程序,我必须在表格行内动态放置一个按钮。问题是我创建的按钮被拉伸(stretch),如下图所示:

screenshot

我还在下面放置了一些应用程序代码,以便您更好地理解。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_punteggio);
    showAlertDialog_modality(R.layout.dialog_change_modality);
    final TableLayout tableLayout           = findViewById(R.id.tableLayout);
    final RelativeLayout relativeLayout           = findViewById(R.id.relativeLayout);
    final Button btn_settings = findViewById(R.id.btn_settings);
    Bundle datipassati = getIntent().getExtras();
    String player = datipassati.getString("players");
    giocatore = player.split("%");
    Log.d("TAG", "array: " + giocatore[0]);

    for (int i = 0; i < giocatore.length; i++) {
        punti[i] = 0;
        TableRow tbrow = new TableRow(this);
        final TextView t3v = new TextView(this);
        txPunti[i] = t3v;
        //------------------------- Textview Player
        final TextView t1v = new TextView(this);
        t1v.setText(giocatore[i].toUpperCase());
        t1v.setTextColor(Color.BLACK);
        t1v.setGravity(Gravity.CENTER);
        t1v.setTextSize(20);
        t1v.setWidth(400);
        tbrow.addView(t1v);
        //-------------------- BTN MENO
        Button btnMeno = new Button(this);
        btnMeno.setText("-");
        btnMeno.setTextColor(Color.BLACK);
        btnMeno.setGravity(Gravity.CENTER);
        tbrow.addView(btnMeno);
        btnMeno.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
               removepoint(t3v);
            }
        });
        // --------------------- TEXT VIEW PUNTI
        t3v.setText(punti[i]+"");
        t3v.setTextSize(25);
        t3v.setMaxWidth(150);
        t3v.setPadding(20,0,20,0);
        t3v.setTypeface(t3v.getTypeface(), Typeface.BOLD);
        t3v.setTextColor(Color.RED);
        t3v.setGravity(Gravity.CENTER);
        tbrow.addView(t3v);
        //----------------------------- BTN PIU
        Button btnPiu = new Button(this);
        btnPiu.setBackground(ContextCompat.getDrawable(Activity_punteggio.this, R.drawable.ic_add_circle_black_24dp));
        btnPiu.setTextColor(Color.BLACK);
        btnPiu.setGravity(Gravity.CENTER);
        tbrow.addView(btnPiu);
        btnPiu.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                addPoint(t3v, t1v);
            }
        });
        tableLayout.addView(tbrow);
    }
    btn_settings.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showAlertDialog_modality(R.layout.dialog_change_modality);
        }
    });
}

这也是 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activity_punteggio">

<Button
    android:id="@+id/btn_settings"
    android:layout_width="55dp"
    android:layout_height="32dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="0dp"
    android:layout_marginRight="1dp"
    android:background="@android:color/transparent"
    android:drawableBottom="@drawable/ic_settings_black_24dp" />

<TextView
    android:id="@+id/title_player"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:text="Chip-Chop"
    android:textSize="25dp"
    android:textStyle="bold|italic" />

<TableLayout
    android:id="@+id/tableLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="80dp">
</TableLayout>


</RelativeLayout>

希望你能帮助我。

最佳答案

Please check this solution. setImageResource is the method assign image resource. Set background method is recommended for the background of the button.

ImageButton btnPiu = new ImageButton(this);

btnPiu.setImageDrawable(ContextCompat.getDrawable(Activity_punteggio.this, R.drawable.ic_add_circle_black_24dp));

btnPiu.setTextColor(Color.BLACK);
btnPiu.setGravity(Gravity.CENTER);
tbrow.addView(btnPiu);
btnPiu.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        addPoint(t3v, t1v);
    }
});
tableLayout.addView(tbrow);

I used ImageButton other than Button. setImageDrawable

关于java - 我无法在表格 View 内设置按钮宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60681916/

相关文章:

android - 如何从 Activity 中获取结果代码?

android - 我可以设置android :layout_toLeftOf programmatically?吗

java - 如何监听 ACTION_MANAGE_OVERLAY_PERMISSION 是否允许

java - 如何在 ArrayList 中搜索相似之处

多行 Java 标签

android - 关于刷写安卓启动镜像

java - 如何在数组中正确添加JTextField组件?

android - RecyclerView拖动第一个项目时,它跳到底部

java - Android 文本冒险应用程序 - 如何根据整数绘制不同的 XML 布局内容

android - XXHDPI 布局在不同的 XXHDPI 设备上不同