java - layout_weight 不起作用

标签 java android xml android-layout android-layout-weight

在我的应用程序中,我有一个如图所示的 Activity (图 #1)。 在 TableLayout 中,我将构建一个包含 n 行和 6 列的表。 我构建表格插入,就像列表一样,n LinearLayout 并且每个包含 6 个 TextView(表格的值)和 5 个 TextView,它们充当列之间的空格。 我写了这个,custom_linearlayout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/custom_linearLayout" >
    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="4"
        android:id="@+id/titolo1"
        android:text="tit1"
        android:background="#4584d3"
        />
    <TextView
        android:layout_width="2dp"
        android:layout_height="match_parent"
        android:id="@+id/spazio1"
        />
    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/titolo2"
        android:text="tit2"
        android:background="#4584d3"
        />
    <TextView
        android:layout_width="2dp"
        android:layout_height="match_parent"
        android:id="@+id/spazio2"
        />
    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/titolo3"
        android:text="tit3"
        android:background="#4584d3"
        />
    <TextView
        android:layout_width="2dp"
        android:layout_height="match_parent"
        android:id="@+id/spazio3"
        />
    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/titolo4"
        android:text="tit4"
        android:background="#4584d3"
        />
    <TextView
        android:layout_width="2dp"
        android:layout_height="match_parent"
        android:id="@+id/spazio4"
        />
    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/titolo5"
        android:text="tit5"
        android:background="#4584d3"
        />
    <TextView
        android:layout_width="2dp"
        android:layout_height="match_parent"
        android:id="@+id/spazio5"
        />
    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/titolo6"
        android:text="tit6"
        android:background="#4584d3"
        />
</LinearLayout>

渲染应该是这样的:(图 #2)

这是使用布局的java代码:

package blablabla.myapplication;
import android.content.Context;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;
import blablabla.myapplication.R;

public class Riga extends LinearLayout {
    TextView colonna1, colonna2 , colonna3, colonna4, colonna5, colonna6;

    public Riga (Context contesto) {
        super (contesto);
        init ();
    }

    private void init () {
        LinearLayout ll = (LinearLayout) LayoutInflater.from (getContext()).inflate(R.layout.custom_linearlayout , null);
        colonna1 = (TextView)ll.findViewById(R.id.titolo1);
        colonna2 = (TextView)ll.findViewById(R.id.titolo2);
        colonna3 = (TextView)ll.findViewById(R.id.titolo3);
        colonna4 = (TextView)ll.findViewById(R.id.titolo4);
        colonna5 = (TextView)ll.findViewById(R.id.titolo5);
        colonna6 = (TextView)ll.findViewById(R.id.titolo6);
        addView(ll);
    }

    public void setVal1 (String s) {
        colonna1.setText(s);
    }

    public void setVal2 (String s) {
        colonna2.setText(s);
    }

    public void setVal3 (String s) {
        colonna3.setText(s);
    }

    public void setVal4 (String s) {
        colonna4.setText(s);
    }

    public void setVal5 (String s) {
        colonna5.setText(s);
    }

    public void setVal6 (String s) {
        colonna6.setText(s);
    }

    public void setValues (String [] array) {
        if (array.length == 6) {
            colonna1.setText(array[0]);
            colonna2.setText(array[1]);
            colonna3.setText(array[2]);
            colonna4.setText(array[3]);
            colonna5.setText(array[4]);
            colonna6.setText(array[5]);
        }
    }


    public void setBackgroundColor (int colore) {
        colonna1.setBackgroundColor(colore);
        colonna2.setBackgroundColor(colore);
        colonna3.setBackgroundColor(colore);
        colonna4.setBackgroundColor(colore);
        colonna5.setBackgroundColor(colore);
        colonna6.setBackgroundColor(colore);
    }

    public void setForegroundColor (int colore) {
        colonna1.setTextColor(colore);
        colonna2.setTextColor(colore);
        colonna3.setTextColor(colore);
        colonna4.setTextColor(colore);
        colonna5.setTextColor(colore);
        colonna6.setTextColor(colore);
    }
}

在 Activity 中我称这些指令为:

TableLayout tl = (TableLayout)findViewById(R.id.tableLayout);
Riga rigaTitoli = new Riga (this);
tl.addView(rigaTitoli);

但我有这个(图 #3)

image

错在哪里?我该如何解决?

最佳答案

LinearLayout ll = (LinearLayout) LayoutInflater.from (getContext()).inflate(R.layout.custom_linearlayout , null);

您传递一个 null 作为父 View ,这使得 match_parent 调整您膨胀的 LinearLayout 不起作用。考虑将 this 作为根传递,并删除 addView(),因为默认情况下 inflater 会将膨胀 View 添加到非空父级。

关于java - layout_weight 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27839811/

相关文章:

java - 为什么分支预测比完全没有分支要快?

Android - 如何添加简单的页脚?

javascript - 使用普通 javascript 的 jQuery 将 XML 转换为对象

java - SOAP 请求 XML 中的 CDATA 部分

java - Struts2 从 struts.xml 文件执行一个 Action 的多个结果

java - 如何从 Redis 缓存中获取自定义值?

java - 从java中的特殊响应模式解析字符串值

java - 图的可视化

android - 保存和恢复 ExpandableListActivity 的展开/折叠状态

android - Android中如何处理networkonmainthreadexception?