创建不需要在XML

时间:2017-07-01 10:31:31

标签: android xml

是否可以创建一个自定义视图,以便

引用它
<components.layouts.CustomView
    android:text="@string/sign_in_options" />

没有在xml中明确说明layout_width和layout_height,因为这已经在CustomView类中定义了

public class CustomView extends TextView {

    public CustomView(Context context) {
        super(context);
        setup();
    }

    public CustomView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setup();
    }

    public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setup();
    }

    public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        setup();
    }

    private void setup() {
        setLayoutParams(new ConstraintLayout.LayoutParams(
                ConstraintLayout.LayoutParams.MATCH_PARENT,
                ConstraintLayout.LayoutParams.WRAP_CONTENT));

        setBackgroundColor(getResources().getColor(R.color.background));
        setTextAlignment(TextView.TEXT_ALIGNMENT_CENTER);
        setAllCaps(true);

        int i = (int)getResources().getDimension(R.dimen.inner_space);
        setPadding(i, i, i, i);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        ViewGroup.MarginLayoutParams margins = ViewGroup.MarginLayoutParams.class.cast(getLayoutParams());
        int h = (int)getResources().getDimension(R.dimen.horizontal_space);
        margins.setMargins(0, h, 0, h);
        setLayoutParams(margins);
    }
}

有人知道是否有办法吗?

2 个答案:

答案 0 :(得分:1)

试试这个:

List<UserModel>

答案 1 :(得分:0)

你可以试试下面的东西。我使用此方法以编程方式插入视图。你可以根据你的要求做。

View insertPhoto(String path) {
    Bitmap bm = ImageUtils.decodeSampledBitmapFromUri(path, 100, 100);

    // Main Layout(Linear)
    layout = new LinearLayout(getApplicationContext());
    layout.setLayoutParams(new ViewGroup.LayoutParams(170, ViewGroup.LayoutParams.MATCH_PARENT));
    layout.setGravity(Gravity.CENTER);
    layout.setOrientation(LinearLayout.VERTICAL);

    // Sub Layout(Relative)
    relativeLayout = new RelativeLayout(getApplicationContext());
    relativeLayout.setLayoutParams(new ViewGroup.LayoutParams(150, 150));

    // Photo (ImageView)
    RelativeLayout.LayoutParams imgParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    imgParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    imgParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    imgParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    imgParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

    imageView = new ImageView(getApplicationContext());
    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    imageView.setBackground(getResources().getDrawable(R.drawable.img_bg));
    imageView.setImageBitmap(bm);
    imageView.setBackgroundColor(ImageUtils.getDominantColor(bm));
    //Log.e("", imageView.getId() + "     +++++");

    // Sync Icon (ImageView)
    RelativeLayout.LayoutParams iconParams = new RelativeLayout.LayoutParams(50, 50);
    iconParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    iconParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

    syncIcon = new ImageView(getApplicationContext());

    //syncIcon.setTag(imageName);
    //Log.e("tag", syncIcon.getTag() + "    +++++");
    //syncIcon.setImageResource(R.drawable.ic_cloud_done);
    //syncIcon.setImageResource(R.drawable.ic_cloud_off);
    syncIcon.setImageResource(R.drawable.ic_cloud_queue);
    //syncIcon.setImageResource(R.drawable.ic_cloud_upload);
    //syncIcon.setImageResource(R.drawable.ic_sync);
    //syncIcon.setImageResource(R.drawable.ic_alert);
    //syncIcon.setImageResource(R.drawable.ic_action_tick);

    //imageView.setLayoutParams(imgParams);
    //syncIcon.setLayoutParams(iconParams);

    // Placing both ImageViews inside Relative Layout
    relativeLayout.addView(imageView, imgParams);
    relativeLayout.addView(syncIcon, iconParams);

    // Placing Relative Layout finally inside Linear Layout
    layout.addView(relativeLayout);

    return layout;
}