简体   繁体   English

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

[英]Views within custom layout won't show

I'm creating a custom linear layout to hold two image views. 我正在创建一个自定义线性布局以容纳两个图像视图。 I first thought that the layout is not showing at all but I then set its background to black to check if the layout is being inflated and it did. 我首先以为布局根本没有显示,但随后将其背景设置为黑色以检查布局是否被充气,并且确实如此。 Then understood that the problem is the image views, they simply not showing. 然后了解到问题在于图像视图,它们根本没有显示。

Thanks in advance :-) 提前致谢 :-)

This is the class: 这是课程:

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);
    }
}

And this is the xml: 这是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>

It seems that the View object acted like match_parent and had height of nearly 400 pixels. 看来View对象的行为类似于match_parent,并且高度接近400像素。 This caused the image views to drop outside the boundaries of the screen. 这导致图像视图落在屏幕边界之外。 Once I removed the View the image views turned up. 删除视图后,图像视图就会打开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM