在表视图中添加布局

时间:2016-02-18 17:56:27

标签: java android

我在xml中创建了一个代码,使用tablerow创建了一个tablelayou,每个tablerow都有3个线性布局。 第一个线性布局的宽度= 40dp,高度= 90dp。第二个linearlayout的宽度= 0dp,高度= 90dp,权重= 1,第三个linearlayout等于de first。所以我有这个结果:

xml layout

所以我需要在java上创建相同的布局,我使用相同的配置但是对齐很糟糕。我该怎么做呢?看,

enter image description here

我把边框显示对齐。这是java代码:

 int wlienar = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());
    int hlienar = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 90, getResources().getDisplayMetrics());
    int wLienarUserDataBeer = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 0, getResources().getDisplayMetrics());
    int hLienarUserDataBeer = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 90, getResources().getDisplayMetrics());
    int wUserPicture = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());
    int hUserPicture = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());
    int wLogoBeer = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());
    int hLogoBeer = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());

    int margim = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics());

    Bitmap bmImage = BitmapFactory.decodeResource(getResources(),R.mipmap.eu);

    iconViewRound.setImageBitmap(bmIcon);
    imageViewRound.setImageBitmap(bmImage);

     /* Find Tablelayout defined in main.xml */
    TableLayout tl = (TableLayout) findViewById(R.id.feeds_table);

    GradientDrawable border = new GradientDrawable();
    border.setColor(0xFFFFFFFF); //white background
    border.setStroke(1, 0xFF000000); //black border with full opacity

    for(int i=0 ; i < 10 ; i++) {
     /*create a linear layout to user picture*/
        LinearLayout linearUserPicture = new LinearLayout(this);
        linearUserPicture.setOrientation(LinearLayout.VERTICAL);
        linearUserPicture.setBackground(border);

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(wlienar, hlienar);

        layoutParams.setMargins(0, 0, margim, 0);
       // layoutParams.gravity = Gravity.NO_GRAVITY;

        RoundedImageView imageView = new RoundedImageView(this, null);
        imageView.setImageResource(R.mipmap.eu);
        //setting image position
        imageView.setLayoutParams(new ViewGroup.MarginLayoutParams(wUserPicture, hUserPicture));

        linearUserPicture.addView(imageView, layoutParams);
        /*create a linear layout to user comments beer*/
        //user name
        TextView textViewUserName = new TextView(this);
        if (Build.VERSION.SDK_INT < 23) {
            textViewUserName.setTextAppearance(this, R.style.feeds_user_connected_style);
        } else {
            textViewUserName.setTextAppearance(R.style.feeds_user_connected_style);
        }
        textViewUserName.setText(R.string.user_feed_name);

        // beer in date
        TextView textViewDateBeerIn = new TextView(this);
        if (Build.VERSION.SDK_INT < 23) {
            textViewDateBeerIn.setTextAppearance(this, R.style.dateBeeinAndPlace);
        } else {
            textViewDateBeerIn.setTextAppearance(R.style.dateBeeinAndPlace);
        }
        textViewDateBeerIn.setText(R.string.date_beerin);

        // textview beer
        TextView textViewBeer = new TextView(this);
        if (Build.VERSION.SDK_INT < 23) {
            textViewBeer.setTextAppearance(this, R.style.beerBeerin);
        } else {
            textViewBeer.setTextAppearance(R.style.beerBeerin);
        }
        textViewBeer.setText(R.string.beerBeerin);

        //textview place beer in
        TextView textViewPlace = new TextView(this);
        if (Build.VERSION.SDK_INT < 23) {
            textViewPlace.setTextAppearance(this, R.style.dateBeeinAndPlace);
        } else {
            textViewPlace.setTextAppearance(R.style.dateBeeinAndPlace);
        }
        textViewPlace.setText(R.string.place_beerin);

        //textview place count comments
        TextView textViewCountComments = new TextView(this);
        if (Build.VERSION.SDK_INT < 23) {
            textViewCountComments.setTextAppearance(this, R.style.countBeerinComment);
        } else {
            textViewCountComments.setTextAppearance(R.style.countBeerinComment);
        }
        textViewCountComments.setText(R.string.countBeerinComment);

        LinearLayout lienarComponents = new LinearLayout(this);
        lienarComponents.setOrientation(LinearLayout.VERTICAL);

        lienarComponents.addView(textViewUserName);
        lienarComponents.addView(textViewDateBeerIn);
        lienarComponents.addView(textViewBeer);
        lienarComponents.addView(textViewPlace);
        lienarComponents.addView(textViewCountComments);

        LinearLayout linearUserDataBeer = new LinearLayout(this);
        linearUserDataBeer.setOrientation(LinearLayout.VERTICAL);
        linearUserDataBeer.setBackground(border);
        LinearLayout.LayoutParams layoutParamsUserDataBeer = new LinearLayout.LayoutParams(0, hLienarUserDataBeer,1);
        linearUserDataBeer.addView(lienarComponents, layoutParamsUserDataBeer);
        //logo beer
        /*create a linear layout to logo beer*/
        LinearLayout linearLogoBeer = new LinearLayout(this);
        linearLogoBeer.setOrientation(LinearLayout.HORIZONTAL);
        linearLogoBeer.setBackground(border);
        LinearLayout.LayoutParams layoutParamsLogo = new LinearLayout.LayoutParams(wlienar, hlienar);

        //ImageView Setup
        ImageView imageViewLogoBeer = new ImageView(this);
        imageViewLogoBeer.setLayoutParams(
                new ViewGroup.LayoutParams(
                        // or ViewGroup.LayoutParams.WRAP_CONTENT
                        wLogoBeer,
                        // or ViewGroup.LayoutParams.WRAP_CONTENT,
                        hLogoBeer));

        imageViewLogoBeer.setImageResource(R.mipmap.beerin);
        linearLogoBeer.addView(imageViewLogoBeer, layoutParamsLogo);

        TableRow tr = new TableRow(this);
        tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        //tr.setBackgroundResource(R.drawable.border);
        tr.setBackground(border);

        tr.addView(linearUserPicture);
        tr.addView(linearUserDataBeer);
        tr.addView(linearLogoBeer);
        tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));

    }

2 个答案:

答案 0 :(得分:0)

您需要向LienarLayout提供LayoutParams。将layout_width设置为match_parent,它将填充单元格的中间部分。但是还有一个问题:TextViews应该推送LinearLayout,但它们不会。问题是您正在构建没有属性的所有这些视图。例如,TextView有几个构造函数。对于代码实例化,您应该使用

TextView myTextView = new TextView(context, attributeSet);

其中attributeSet是AttributeSet(http://developer.android.com/reference/android/util/AttributeSet.html)。这将允许您以与在xml中相同的方式操作TextView。到目前为止,他们可能都有默认宽度,不剪辑其内容,所以一切都很好。通过使用attributeSet,您也可以控制textviews宽度。

EDIT :: 我发现我错了,你在调用addView()时提供了LayoutParams。

在这一行:

 LinearLayout.LayoutParams layoutParamsUserDataBeer = new LinearLayout.LayoutParams(0, hLienarUserDataBeer,1);

将宽度设置为0,将权重设置为1,这与其他布局设置为WRAP_CONTENT的事实冲突。 请尝试改为:

 LinearLayout.LayoutParams layoutParamsUserDataBeer = new LinearLayout.LayoutParams(ViewGroup.MATCH_PARENT, hLienarUserDataBeer,0);

答案 1 :(得分:0)

在katzenhut和我的朋友Afonso的帮助下

在我的tablerow中我有3列,所以第一列是数字0,第二列是数字1,第三列是数字2,就像一个列从0开始的数组。所以默认情况下,表格布局中的列是WRAP_CONTENT ,在表格布局中应该设置为MATCH_PARENT的列,在我的情况下第二列是MATCH_PARENT,所以我添加这一行:

tl.setColumnStretchable( 1, true );

对齐保持良好,完整代码:

TableLayout tl = (TableLayout) findViewById(R.id.feeds_table);
    tl.setColumnStretchable( 1, true ); // this line fix the error

      for(int i=0 ; i < 10 ; i++) {
     /*create a linear layout to user picture*/
        LinearLayout linearUserPicture = new LinearLayout(this);
        linearUserPicture.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(wlienar, hlienar);

        layoutParams.setMargins(0, 0, margim, 0);

        RoundedImageView imageView = new RoundedImageView(this, null);
        imageView.setImageResource(R.mipmap.eu);

        //setting image position
        imageView.setLayoutParams(new ViewGroup.MarginLayoutParams(wUserPicture, hUserPicture));

        linearUserPicture.addView(imageView, layoutParams);

        /*create a linear layout to user comments beer*/

        //user name
        TextView textViewUserName = new TextView(this);
        if (Build.VERSION.SDK_INT < 23) {
            textViewUserName.setTextAppearance(this, R.style.feeds_user_connected_style);
        } else {
            textViewUserName.setTextAppearance(R.style.feeds_user_connected_style);
        }
        textViewUserName.setText(R.string.user_feed_name);

        // beer in date
        TextView textViewDateBeerIn = new TextView(this);
        if (Build.VERSION.SDK_INT < 23) {
            textViewDateBeerIn.setTextAppearance(this, R.style.dateBeeinAndPlace);
        } else {
            textViewDateBeerIn.setTextAppearance(R.style.dateBeeinAndPlace);
        }
        textViewDateBeerIn.setText(R.string.date_beerin);

        // textview beer
        TextView textViewBeer = new TextView(this);
        if (Build.VERSION.SDK_INT < 23) {
            textViewBeer.setTextAppearance(this, R.style.beerBeerin);
        } else {
            textViewBeer.setTextAppearance(R.style.beerBeerin);
        }
        textViewBeer.setText(R.string.beerBeerin);

        //textview place beer in
        TextView textViewPlace = new TextView(this);
        if (Build.VERSION.SDK_INT < 23) {
            textViewPlace.setTextAppearance(this, R.style.dateBeeinAndPlace);
        } else {
            textViewPlace.setTextAppearance(R.style.dateBeeinAndPlace);
        }
        textViewPlace.setText(R.string.place_beerin);

        //textview place count comments
        TextView textViewCountComments = new TextView(this);
        if (Build.VERSION.SDK_INT < 23) {
            textViewCountComments.setTextAppearance(this, R.style.countBeerinComment);
        } else {
            textViewCountComments.setTextAppearance(R.style.countBeerinComment);
        }
        textViewCountComments.setText(R.string.countBeerinComment);

        //adicionado os camponentes no linear que irá ajuntar todos no linear principal
        LinearLayout lienarComponents = new LinearLayout(this);
        lienarComponents.setOrientation(LinearLayout.VERTICAL);

        lienarComponents.addView(textViewUserName);
        lienarComponents.addView(textViewDateBeerIn);
        lienarComponents.addView(textViewBeer);
        lienarComponents.addView(textViewPlace);
        lienarComponents.addView(textViewCountComments);

        LinearLayout linearUserDataBeer = new LinearLayout(this);
        linearUserDataBeer.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams layoutParamsUserDataBeer = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, hLienarUserDataBeer);
        linearUserDataBeer.addView(lienarComponents, layoutParamsUserDataBeer);

        //logo beer

        /*create a linear layout to logo beer*/
        LinearLayout linearLogoBeer = new LinearLayout(this);
        linearLogoBeer.setOrientation(LinearLayout.HORIZONTAL);
        LinearLayout.LayoutParams layoutParamsLogo = new LinearLayout.LayoutParams(wlienar, hlienar);

        //ImageView Setup
        ImageView imageViewLogoBeer = new ImageView(this);


        imageViewLogoBeer.setLayoutParams(
                new ViewGroup.LayoutParams(
                        // or ViewGroup.LayoutParams.WRAP_CONTENT
                        wLogoBeer,
                        // or ViewGroup.LayoutParams.WRAP_CONTENT,
                        hLogoBeer));

        imageViewLogoBeer.setImageResource(R.mipmap.beerin);

        linearLogoBeer.addView(imageViewLogoBeer, layoutParamsLogo);

        TableRow tr = new TableRow(this);
        tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        tr.setBackgroundResource(R.drawable.border);

        tr.addView(linearUserPicture);
        tr.addView(linearUserDataBeer);
        tr.addView(linearLogoBeer);
        tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));

    }