开发者社区> 问答> 正文

我无法在表格视图内设置按钮宽度

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

屏幕截图

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

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>

我希望你能帮助我。

问题来源:Stack Overflow

展开
收起
montos 2020-03-26 11:25:30 394 0
1 条回答
写回答
取消 提交回答
  • 请检查此解决方案。setImageResource是分配图像资源的方法。建议将背景设置为按钮的背景。

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

    我使用了Button以外的ImageButton。setImageDrawable

    回答来源:Stack Overflow

    2020-03-26 11:26:05
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载