java - 设置tablerow元素的权重

标签 java android android-layout-weight

我正在尝试以编程方式设置表行中 TextView 和复选框的权重。经过一番研究,我发现 LayoutParams 的第三个参数用于执行此操作。然而,它似乎不起作用。有人可以看一下这段代码并看看问题出在哪里吗?谢谢。 (使用 LinearLayout 建议进行编辑)

public LayerSelectorView(Context context) 
{
    super(context);
    this.context = context;
    float density = context.getResources().getDisplayMetrics().density;

    linearLayout = new LinearLayout(context);
    linearLayout.setOrientation(LinearLayout.VERTICAL);
    final LinearLayout.LayoutParams mainParams = new LinearLayout.LayoutParams(
                             LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    linearLayout.setLayoutParams(mainParams);

    titleText = new TextView(context);
    titleText.setTextAppearance(context, android.R.style.TextAppearance_Large);
    titleText.setText("Select Layers");
    titleText.setTextColor(Color.WHITE);
    titleText.setId(id);

    final LinearLayout.LayoutParams titleParams = new LinearLayout.LayoutParams(
                             LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    titleParams.setMargins(20, 20, 0, 0);
    titleText.setLayoutParams(titleParams);

    linearLayout.addView(titleText);
}
public void updateView()
{
    overlays = ((MapActivity)context).getMapView().getOverlays();

    loadLayers();

    this.addView(linearLayout);

    linearLayout.setBackgroundColor(0x88000000);
    linearLayout.invalidate();
    this.invalidate();
}
public void loadLayers()
{
    TableLayout table = new TableLayout(context);
    final LinearLayout.LayoutParams tableParams = new LinearLayout.LayoutParams(
                             LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    table.setLayoutParams(tableParams);
    int i = id+1;
    for(final MapOverlay mo: overlays)
    {       
        TableRow tblRow = new TableRow(context);

        TextView layer = new TextView(context);
        TableRow.LayoutParams trParams = new TableRow.LayoutParams(0,
                             LayoutParams.FILL_PARENT, 1f);
        layer.setTextAppearance(context, android.R.style.TextAppearance_Large);
        layer.setText(mo.getName());
        layer.setTextColor(Color.WHITE);
        layer.setId(i);
        i++;
        layer.setLayoutParams(trParams);

        final CheckBox chk = new CheckBox(context);
        TableRow.LayoutParams trParams1 = new TableRow.LayoutParams(0, 
                             LayoutParams.FILL_PARENT, .2f);
        chk.setLayoutParams(trParams1);
        chk.setFocusable(false);
        chk.setClickable(false);
        chk.setChecked(true);

        tblRow.addView(layer);  
        tblRow.addView(chk);
        table.addView(tblRow);

        tblRow.setOnClickListener(new OnClickListener(){

            public void onClick(View v) {
                if(mo.isVisible())
                {
                    mo.setVisible(false);
                    chk.setChecked(false);
                }
                else
                {
                    mo.setVisible(true);
                    chk.setChecked(true);
                }
            }
        });
    }
    linearLayout.addView(table);
}

最佳答案

您不能将权重与RelativeLayout一起使用。您需要使用LinearLayout。我不知道这是否对你有用,但我希望它仍然有帮助。

关于java - 设置tablerow元素的权重,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11298503/

相关文章:

android - ExpandableView中如何识别 'onClick view'并获取 'groupId'

android - TableLayout权重和问题

java - 删除最后一行 JTable 的问题

java - Tomcat 7 : Sonar compliant setHttpOnly(true) for Cookie

java - 如何使用Button自动向Firebase进行身份验证 - Java

android - 如何使应用程序响应所有 Android 设备?

android - 在android上设置计算器按钮的布局

java - Spring Boot 应用程序中日志记录概念的行数

java.lang.StringIndexOutOfBoundsException : String index out of range: 10 ---. length() 循环

java - 如何使用特定基数将任何有效字符串解析为整数?