2014-01-05 4 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 пикселей. Это привело к тому, что изображения отображались за пределами экрана. Как только я удалил View View View.

Смежные вопросы