【问题标题】:Android - Custom View and ImageView on XML but Custom View not visibleAndroid - XML 上的自定义视图和 ImageView 但自定义视图不可见
【发布时间】:2013-12-30 15:53:11
【问题描述】:

我尝试使用 setContentView (R.layout.main) 来显示图像视图和自定义视图。不知何故,只能显示图像视图,但自定义视图不可见(即位图 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);
}
}

主活动

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>

使用上面的代码,只显示屏幕图像...

【问题讨论】:

  • 我会说因为你的 ImageView 的高度设置为填充父级,所以你的 TitleView 没有空间了。
  • 在我将 ImageView 的高度更改为 wrap_content 之后,我有了一点改善,现在我可以成为自定义视图的一部分了。但我希望 Bitmap 对象(即 ButtonStart)位于 ImageView 之上,而不是 ImageView 之下。比如在图片(ImageView)上绘制一个按钮(自定义视图)。我应该怎么办? @MagicMicky
  • @Wallyfull android:orientation="vertical" 所以它的工作方式与现在一样。使用 FrameLayout 或 RelativeLayout。
  • @Raghunandan 非常感谢。它按我现在想要的方式工作。新年快乐!

标签: android xml android-custom-view ondraw


【解决方案1】:
<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 之外

【讨论】:

    猜你喜欢
    • 2013-04-18
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多