ImageView不会显示

发布于 2024-11-28 17:53:07 字数 2216 浏览 2 评论 0原文

在OnCreate中:

svMaster = (ScrollView) findViewById(R.id.scroller);     //Only layout in XML file
    svMaster.setVerticalFadingEdgeEnabled(false);
    Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    height = display.getHeight();

...

initializeGUI();
svMaster.removeAllViews();
svMaster.addView(llMaster);

在initializeGUI()中:

llMaster = new LinearLayout(this);         //Only direct child of scrollview
LinearLayout llFirstScreen = new LinearLayout(this);  //First layout added to llMaster;
                                                      //It's size is that of one screen
LinearLayout.LayoutParams lpMaster = new LinearLayout.LayoutParams
            (LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

LinearLayout.LayoutParams lpFirstScreen = new LinearLayout.LayoutParams
            (LayoutParams.MATCH_PARENT, height);

    llMaster.setLayoutParams(lpMaster);
    llMaster.setOrientation(LinearLayout.VERTICAL);

    llFirstScreen.setLayoutParams(lpFirstScreen);
    llFirstScreen.setBackgroundResource(R.color.blue2);
    llFirstScreen.setOrientation(LinearLayout.VERTICAL);

这是我与ImageView相关的代码:

ImageView ivWeather = new ImageView(this);

LinearLayout.LayoutParams ivWeatherParams = new LinearLayout.LayoutParams
            (Scale(80), Scale(80);

ivWeather.setImageResource(R.drawable.sunny);
Log.d("ImageView loading?", "I hope so");
ivWeather.setScaleType(ScaleType.FIT_XY);
ivWeather.setLayoutParams(ivWeatherParams);
ivWeather.setVisibility(View.VISIBLE);      //This is code I tried
ivWeather.setFocusable(true);               //when it wouldn't show up
ivWeather.setFocusableInTouchMode(true);    //...
ivWeather.invalidate();                     //...

...

llFirstScreen.addView(ivWeather);
Log.d("ImageView loading?", "I hope so");
llMaster.addView(llFirstScreen);

...

我已经毫无问题地将TextView添加到llFirstScreen,我想知道为什么ImageView 不会出现。我什至尝试将 llFirstScreen 的高度调整为 WRAP_CONTENT 而不是屏幕的高度。所做的只是将布局缩小到两个 TextView。就好像它从未添加过 ImageView 一样。我输入的日志已签出,因此我知道代码正在运行。 我错过了什么吗?

In OnCreate:

svMaster = (ScrollView) findViewById(R.id.scroller);     //Only layout in XML file
    svMaster.setVerticalFadingEdgeEnabled(false);
    Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    height = display.getHeight();

...

initializeGUI();
svMaster.removeAllViews();
svMaster.addView(llMaster);

In initializeGUI():

llMaster = new LinearLayout(this);         //Only direct child of scrollview
LinearLayout llFirstScreen = new LinearLayout(this);  //First layout added to llMaster;
                                                      //It's size is that of one screen
LinearLayout.LayoutParams lpMaster = new LinearLayout.LayoutParams
            (LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

LinearLayout.LayoutParams lpFirstScreen = new LinearLayout.LayoutParams
            (LayoutParams.MATCH_PARENT, height);

    llMaster.setLayoutParams(lpMaster);
    llMaster.setOrientation(LinearLayout.VERTICAL);

    llFirstScreen.setLayoutParams(lpFirstScreen);
    llFirstScreen.setBackgroundResource(R.color.blue2);
    llFirstScreen.setOrientation(LinearLayout.VERTICAL);

Here's my code pertaining to the ImageView:

ImageView ivWeather = new ImageView(this);

LinearLayout.LayoutParams ivWeatherParams = new LinearLayout.LayoutParams
            (Scale(80), Scale(80);

ivWeather.setImageResource(R.drawable.sunny);
Log.d("ImageView loading?", "I hope so");
ivWeather.setScaleType(ScaleType.FIT_XY);
ivWeather.setLayoutParams(ivWeatherParams);
ivWeather.setVisibility(View.VISIBLE);      //This is code I tried
ivWeather.setFocusable(true);               //when it wouldn't show up
ivWeather.setFocusableInTouchMode(true);    //...
ivWeather.invalidate();                     //...

...

llFirstScreen.addView(ivWeather);
Log.d("ImageView loading?", "I hope so");
llMaster.addView(llFirstScreen);

...

I have added TextViews with no problem to llFirstScreen, and I am wondering why ImageView
won't show up. I tried even adjusting llFirstScreen's height to WRAP_CONTENT instead of the screen's height. All that did was shrink the layout to the two TextViews. It's as if it never added the ImageView. The Logs I put in check out, so I know the code is running.
Am I missing anything?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假扮的天使 2024-12-05 17:53:07

您是否尝试过将 LinearLayout.LayoutParams 从 Scale(80)、Scale(80) 更改为 LayoutParams.WRAP_CONTENT、LayoutParams.FILL_PARENT 等?我从未见过使用 Scale(int) 。如果这是自定义方法,您可以发布吗?

Have you tried changing your LinearLayout.LayoutParams from Scale(80), Scale(80) to something like LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT or something? I've never seen Scale(int) used. If that is a custom method can you post it?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文