自定义窗口小部件的宽度不会填充可用空间

时间:2015-08-16 19:11:23

标签: android android-widget


我为TextView创建了一个自定义小部件。通过xml声明时,创建的自定义组件工作正常。但是当我尝试通过代码动态地将其添加到布局时,它不会填充可用的宽度。
任何人都可以帮助我如何使它占据全宽。

我的xml布局如下:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
<LinearLayout 
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:id="@+id/llayout"
 >

在Activity文件中,我尝试将自定义窗口小部件添加到线性布局中,如下所示:

String yourText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " +
                    "Ut volutpat interdum interdum. Nulla laoreet lacus diam, vitae " +
                    "sodales sapien commodo faucibus. Vestibulum et feugiat enim. Donec " +
                    "semper mi et euismod tempor. Sed sodales eleifend mi id varius. Nam " +
                    "et ornare enim, sit amet gravida sapien. Quisque gravida et enim vel " +
                    "volutpat. Vivamus egestas ut felis a blandit. Vivamus fringilla " +
                    "dignissim mollis. Maecenas imperdiet interdum hendrerit. Aliquam" +
                    " dictum hendrerit ultrices. Ut vitae vestibulum dolor. Donec auctor ante" +
                    " eget libero molestie porta. Nam tempor fringilla ultricies. Nam sem " +
                    "lectus, feugiat eget ullamcorper vitae, ornare et sem. Fusce dapibus ipsum" +
                    " sed laoreet suscipit. ";
         LinearLayout llayout = (LinearLayout)findViewById(R.id.llayout);
         ExpandableTextView expandableTextView = new ExpandableTextView(getApplicationContext());
         LayoutParams lpView = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
         expandableTextView.setLayoutParams(lpView);
         expandableTextView.setTextColor(Color.MAGENTA);
         expandableTextView.setTextSize(25);
         expandableTextView.setText(yourText);
         llayout.addView(expandableTextView);

ExpandableTextView

/**
 * 
 */
package com.andr.expandabletextviewwidget;



import com.andr.kamal56.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.SpannableStringBuilder;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;


     public class ExpandableTextView extends TextView{

    private static final int DEFAULT_TRIM_LENGTH = 200;
    private static final String ELLIPSIS = ".....";

    private CharSequence originalText;
    private CharSequence trimmedText;
    private BufferType bufferType;
    private boolean trim = true;
    private int trimLength;

    public ExpandableTextView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }


    public ExpandableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ExpandableTextView);
        this.trimLength = typedArray.getInt(R.styleable.ExpandableTextView_trimLength, DEFAULT_TRIM_LENGTH);
        typedArray.recycle();

        setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                trim = !trim;
                setText();
                requestFocusFromTouch();                
            }
        });
    }

     private void setText() {
            super.setText(getDisplayableText(), bufferType);
        }

    private CharSequence getDisplayableText() {
        return trim ? trimmedText : originalText;
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        originalText = text;
        trimmedText = getTrimmedText(text);
        bufferType = type;
        setText();

    }



     private CharSequence getTrimmedText(CharSequence text) {
            if (originalText != null && originalText.length() > trimLength) {
                return new SpannableStringBuilder(originalText, 0, trimLength + 1).append(ELLIPSIS);
            } else {
                return originalText;
            }
        }


     public CharSequence getOriginalText() {
            return originalText;
        }

        public void setTrimLength(int trimLength) {
            this.trimLength = trimLength;
            trimmedText = getTrimmedText(originalText);
            setText();
        }

        public int getTrimLength() {
            return trimLength;
        }
}

1 个答案:

答案 0 :(得分:0)

尝试在您的活动代码中明确使用LinearLayout.LayoutParams。有许多不同的LayoutParams类,有时您可能会错误地使用错误的类,因为导入了错误的类。

LinearLayout.LayoutParams lpView = new LinearLayout.LayoutParams(
        LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);