自定义布局中的视图不会显示

时间:2014-01-05 13:50:01

标签: android android-custom-view

我正在创建一个自定义线性布局来保存两个图像视图。 我首先想到的是布局根本没有显示,但我将其背景设置为黑色以检查布局是否正在膨胀并且确实如此。然后才明白问题是图像视图,它们根本就不显示。

提前致谢: - )

这是班级:

public class LoginIcons extends LinearLayout {

    private ImageView mImageViewLogo;
    private ImageView mImageViewIcons;
    private View mView;
    boolean test = false;

    public LoginIcons(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LoginIcons(Context context) {
        super(context);
        init();
    }

    private void init() {

        setOrientation(LinearLayout.VERTICAL);
        mImageViewLogo = new ImageView(this.getContext());
        mImageViewIcons = new ImageView(this.getContext());
        mView = new View(getContext());

        LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        mImageViewLogo.setLayoutParams(params);

        params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.topMargin = (int)LocalSettingsHelper.dpToPx(getContext(), 20);
        mImageViewIcons.setLayoutParams(params);

        params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.topMargin = (int)LocalSettingsHelper.dpToPx(getContext(), 10);
        mView.setLayoutParams(params);

        mImageViewLogo.setImageResource(R.drawable.splash_logo_placeholder);        
        mImageViewIcons.setImageResource(R.drawable.login_icons);

        mImageViewIcons.setBackgroundColor(Color.BLACK);
        mImageViewLogo.setBackgroundColor(Color.BLACK);

        addView(mView);
        addView(mImageViewLogo);
        addView(mImageViewIcons);

    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();

        setViewsDimensions();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }

    private void setViewsDimensions() {

        LinearLayout parent = (LinearLayout) mImageViewLogo.getParent();

        int screenWidth = LocalSettingsHelper.getScreenWidth(getContext());

        // 0.375 percent
        int imgDim = (int) ((double) screenWidth * 0.32);
        int iconsWidth = (int) ((double) screenWidth * 0.5);

        LinearLayout.LayoutParams params = (LayoutParams) mImageViewLogo
                .getLayoutParams();
        params.width = imgDim;
        params.height = imgDim;
        mImageViewLogo.setLayoutParams(params);

        params = (LayoutParams) mImageViewIcons.getLayoutParams();
        params.width = iconsWidth;
        mImageViewIcons.setLayoutParams(params);
    }
}

这是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="match_parent"
    android:background="@color/HeaderBackground"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button_ok"
        style="@style/LoginOkButtonStyle"
        android:background="@drawable/blue_button_background_selector"
        android:text="@string/ok" />

    <com.example.me.entities.views.LoginIcons
        android:id="@+id/loginIcons"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </com.example.me.entities.views.LoginIcons>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

似乎View对象的行为与match_parent相似,高度接近400像素。这导致图像视图落在屏幕边界之外。 一旦我删除了View,就会显示图像视图。