首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从java代码向Android应用程序添加图片

如何从java代码向Android应用程序添加图片
EN

Stack Overflow用户
提问于 2012-11-20 08:19:36
回答 2查看 14.4K关注 0票数 1

我和我的朋友正在为我们的班级项目制作一个android应用程序。我们正在制作和应用标签。我们想知道是否有一种方法可以为选项卡使用一个xml文件,是否可以直接从java代码添加图片,或者是否需要为每个单独的选项卡创建一个xml文件。如果我们这样做了,我们将如何保持所有选项卡的相同格式。

我提供的代码是我们在其中附加了一些图片的main.xml。tab.xml是我们格式化选项卡的xml布局。SalesExecutiveDashboard.java是调用其他选项卡活动的主要活动。最后,HomeTab.java是我们标签代码上的一个例子。

感谢您提供的所有帮助

main.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
 android:layout_height="fill_parent"
 android:layout_width="fill_parent"
 android:orientation="vertical"
 xmlns:android="http://schemas.android.com/apk/res/android">
<TextView 
 android:layout_height="wrap_content"
 android:layout_width="wrap_content"
 android:layout_margin="1dp" 
 android:text="@string/overall_sales"
 android:id="@+id/pieChartView"/>
<ImageView 
 android:layout_height="fill_parent"
 android:layout_width="fill_parent" 
 android:id="@+id/pie_chart" 
 android:src="@drawable/piechartsmall"/>
</LinearLayout>

tab.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<TabHost
 android:id="@android:id/tabhost" 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_height="fill_parent" 
 android:layout_width="fill_parent"> 

<ScrollView 
 xmlns:android="http://schemas.android.com/apk/res/android"    
 android:layout_height="fill_parent"
 android:layout_width="fill_parent">

<LinearLayout android:id="@+id/LinearLayout01"
 android:layout_height="wrap_content" 
 android:layout_width="fill_parent" 
 android:orientation="vertical">

<HorizontalScrollView 
 android:layout_height="wrap_content"
 android:layout_width="wrap_content" 
 android:scrollbars="none"
 android:fillViewport="true">

<TabWidget android:id="@android:id/tabs"
 android:layout_height="40dp" 
 android:layout_width="wrap_content"
 android:layout_gravity="bottom"
 android:gravity="bottom">

</TabWidget>
</HorizontalScrollView>

<FrameLayout
 android:id="@android:id/tabcontent"
 android:layout_height="fill_parent"
 android:layout_width="fill_parent">
</FrameLayout>
</LinearLayout>
</ScrollView>
</TabHost>

SaleExecutiveDashboard.java

代码语言:javascript
复制
package com.androidpeople.tab;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class SalesExecutiveDashboard extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab);

        /* TabHost will have Tabs */
        //TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);

        TabHost tabHost=getTabHost();

        /* TabSpec used to create a new tab. 
         * By using TabSpec only we can able to setContent to the tab.
         * By using TabSpec setIndicator() we can set name to tab. */

        /* tid1 is firstTabSpec Id. Its used to access outside. */
        TabSpec HomeTabSpec = tabHost.newTabSpec("Tab1");
        TabSpec RevExpTabSpec = tabHost.newTabSpec("Tab2");
        TabSpec AccountTabSpec = tabHost.newTabSpec("Tab3");
        TabSpec DistroTabSpec = tabHost.newTabSpec("Tab4");
        TabSpec SBPTabSpec = tabHost.newTabSpec("Tab5");
        TabSpec AlertTabSpec = tabHost.newTabSpec("Tab6");

        /* TabSpec setIndicator() is used to set name for the tab. */
        /* TabSpec setContent() is used to set content for a particular tab. */
        HomeTabSpec.setIndicator("  Home  ").setContent(new Intent(this,HomeTab.class));

        RevExpTabSpec.setIndicator("  Rev/Exp  ").setContent(new Intent(this,RevExpTab.class));
        AccountTabSpec.setIndicator("  Accounts  ").setContent(new Intent(this,AccountsTab.class));
        DistroTabSpec.setIndicator("  Distribution  ").setContent(new Intent(this,DistroTab.class));
        SBPTabSpec.setIndicator("  Sales by Product  ").setContent(new Intent(this,SBPTab.class));
        AlertTabSpec.setIndicator("  Alerts  ").setContent(new Intent(this,AlertTab.class));

        /* Add tabSpec to the TabHost to display. */
        tabHost.addTab(HomeTabSpec);
        tabHost.addTab(RevExpTabSpec);
        tabHost.addTab(AccountTabSpec);
        tabHost.addTab(DistroTabSpec);
        tabHost.addTab(SBPTabSpec);
        tabHost.addTab(AlertTabSpec);

    }
}

HomeTab.java

代码语言:javascript
复制
package com.androidpeople.tab;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.widget.ImageView;

public class HomeTab extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /* First Tab Content */
        ImageView image = (ImageView) findViewById(R.id.pie_chart);

        //TextView textView = new TextView(this);
        //textView.setText("This is the Home Tab");
        //setContentView(textView);
        setContentView(R.layout.main);

    }
}
EN

回答 2

Stack Overflow用户

发布于 2013-02-01 06:16:57

可以,您可以多次使用一个布局资源。它只指定外观。至于图像,只要它们在您的可绘制资源中,您只需将它们分配到您的ImageView即可。

代码语言:javascript
复制
//hold the integer resource id of your image
Integer img = R.drawable.<<imageid>>;

//get your ImageView
ImageView iView = (ImageView) findViewById(R.id.<<viewname>>);

//assign your image
iView.setImageResource(img);

另一个选择是在可绘制的目录中,你可以创建xml‘选择器’来告诉你的应用程序根据某些条件使用哪个图像。例如,假设您有一个ImageButton,并且想要一个不同的图像来显示它是否被禁用。您可以在可绘制目录中使用如下选择器,以使用可绘制-h/l/mdpi目录中的图像:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/aps/res/android">
    <item android:state_enabled="false" android:drawable="@drawable/<<disabledImage>>"/>
    <item android:drawable="@drawable/<<defaultImage>>"?>
</selector>

然后,您可以在布局中使用此选择器。

代码语言:javascript
复制
<Button android:layout_width="fill_parent" android:id="@+id/btn1" android:text="btn1" android:textSize="20dp" android:textColor="@android:color/transparent" android:background="@drawable/<<selectorName>>" android:layout_height="fill_parent" android:onClick="<<clickevent>>" />

我知道这是一个老生常谈的问题,但希望这能对某些人有所帮助。

票数 1
EN

Stack Overflow用户

发布于 2016-09-05 13:27:52

首先,您需要编辑HomeTab Activity onCreate方法,如下所示:

代码语言:javascript
复制
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        /* First Tab Content */
        ImageView image = (ImageView) findViewById(R.id.pie_chart);

        //TextView textView = new TextView(this);
        //textView.setText("This is the Home Tab");
        //setContentView(textView);
        

    }

我们可以在设置布局文件后转换任何控件,您不能先转换它:)

然后在您的MainActivity content TabHost之后,您可以将其设置为inBuild Listner,如下所示:

代码语言:javascript
复制
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
                @Override
                public void onTabChanged(String arg0) {     
 
    int pos=tabHost.getCurrentTab();  Toast.makeText(this,""+pos,Toast.LENGTH_SHORT).show();
  
  
                    SharedPreferences pref=getSharedPreferences(YOUR_PREFERENC,0);
  
  SharedPreferences.Editor et = pref.edit();
  et.putInt(YOUR_POS_KEY,pos);
  et.commit();
                }       
            });

您可以在子活动和检查位置条件中使用此Java值,以便在一个布局中或仅在一个SharedPreferences文件中设置动态图像

我希望它能起作用

快乐的编码...

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13464754

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档