Android - 自定义视图和XML上的ImageView,但自定义视图不可见

时间:2013-12-30 15:53:11

标签: android xml android-custom-view ondraw

我尝试使用setContentView(R.layout.main)来显示imageview和自定义视图。不知何故,只能显示图像视图,但自定义视图不可见(即Bitmap ButtonStart不可见)。我在其他地方测试了我的onDraw和Texture类的代码(负责加载位图对象)并且它工作正常。所以我不知道出了什么问题...

自定义视图代码

public class TitleView extends View {

private Bitmap ButtonStart;

public TitleView (Context context, AttributeSet attrs) {
    super (context, attrs);
    Texture t2 = new Texture(context);
    t2.loadFromAsset("button_Start.png");
    ButtonStart = t2.getBitmap();
}

@Override
protected void onDraw (Canvas canvas) {
    canvas.drawBitmap (ButtonStart, 0, 0, null);
}
}

MainActivity

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature (Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.main_screen_image);
}
}

main_screen_image.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainScreenLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView 
android:id="@+id/mainScreenImage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop"
android:src="@drawable/screenimage"
>
</ImageView>
<com.lowbband.chimera.TitleView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/titleview"/>
</LinearLayout>

使用上面的代码,只显示了screenimage ...

1 个答案:

答案 0 :(得分:0)

<ImageView 
    android:id="@+id/mainScreenImage"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" //it should be wrap_content otherwise it will take whole space
    android:scaleType="centerCrop"
    android:src="@drawable/screenimage"
    >
    </ImageView>
    <com.lowbband.chimera.TitleView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/titleview"/>

如果图像很大并仍然填满屏幕,则将ScrollView放在LinearLayout

之外