2014-01-05 55 views
1

我正在创建自定义线性布局以容纳两个图像视图。 我首先想到的是布局根本没有显示,但是我将它的背景设置为黑色,以检查布局是否正在充气,并且确实如此。然后明白,问题是图像的意见,他们根本没有显示。自定义布局中的视图不会显示

在此先感谢:-)

这是类:

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> 
+0

显而易见的问题是,如果您测试了解'imgDim'和'iconsWidth'的值是什么。 – Luksprog

+0

是的,价值观是正确的。 – Tsikon

回答

0

看来,查看对象表现得像match_parent和有高度接近400像素。这导致图像视图落在屏幕边界之外。 一旦我删除了查看图像视图打开。