【问题标题】:Setting the param of a button programmatically以编程方式设置按钮的参数
【发布时间】:2021-11-10 00:52:10
【问题描述】:

我正在尝试将 9 按钮添加到我的活动中,我想添加如下按钮:

      <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:layout_columnSpan="1"
            android:layout_rowSpan="1"
            android:text="@string/button1"
            android:layout_margin="5dp"/>

目前我正在使用这个

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Context context = this;
        GridLayout gridBoutons = findViewById(R.id.layout_gridBoutons);
        for(int i = 0; i != 9;i++){
            Bouton bouton = new Bouton(context);
            bouton.setText("");
            GridLayout.LayoutParams param = new GridLayout.LayoutParams();
            param.height = GridLayout.LayoutParams.WRAP_CONTENT;
            param.width = GridLayout.LayoutParams.WRAP_CONTENT;
            bouton.setLayoutParams(param);
            gridBoutons.addView(bouton);
        }
    }

我不知道如何获得列权重行权列跨度和行跨度

【问题讨论】:

    标签: java android android-studio


    【解决方案1】:

    您可以将以下方法与 API 21 和稍后提到的 API 一起使用: Set rowSpan or colSpan of a child of a GridLayout programmatically?

    https://developer.android.com/reference/android/widget/GridLayout.Spec

    规范规范(int start,int size,float weight)

    param.rowSpec = GridLayout.spec(0, 1, (float)1.0);    
    param.columnSpec = GridLayout.spec(0, 1, (float)1.0);
    

    【讨论】:

      猜你喜欢
      • 2011-12-28
      • 2014-09-20
      • 2014-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多