你怎么能在Java code图像添加到Android应用程序 [英] How can you add images to Android App from java code

查看:115
本文介绍了你怎么能在Java code图像添加到Android应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的朋友和我正在一个Android应用程序为我们的课程项目。我们正在和标签应用。我们想知道是否有用于标签一个XML文件中的一种方式,如果我们可以从Java code直接添加图片或做我们需要做一个XML文件,每个单独的标签。如果我们这样做会怎样才好保持相同格式的所有选项卡。

在$ C C I提供是我们附上一些照片到main.xml中。该tab.xml是我们格式化选项卡的XML布局。该SalesExecutiveDashboard.java是调用其他选项卡活动的主要活动。最后HomeTab.java是和例子在我们的标签codeS。

感谢提供的所有帮助。

main.xml中

 <?XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
 机器人:layout_height =FILL_PARENT
 机器人:layout_width =FILL_PARENT
 机器人:方向=垂直
 的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
<的TextView
 机器人:layout_height =WRAP_CONTENT
 机器人:layout_width =WRAP_CONTENT
 机器人:layout_margin =1DP
 机器人:文字=@字符串/ overall_sales
 机器人:ID =@ + ID / pieChartView/>
< ImageView的
 机器人:layout_height =FILL_PARENT
 机器人:layout_width =FILL_PARENT
 机器人:ID =@ + ID / pie_chart
 机器人:SRC =@绘制/ piechartsmall/>
< / LinearLayout中>

tab.xml

 <?XML版本=1.0编码=UTF-8&GT?;
< TabHost
 机器人:ID =@机器人:ID / tabhost
 的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
 机器人:layout_height =FILL_PARENT
 机器人:layout_width =FILL_PARENT><滚动型
 的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
 机器人:layout_height =FILL_PARENT
 机器人:layout_width =FILL_PARENT><的LinearLayout机器人:ID =@ + ID / LinearLayout01
 机器人:layout_height =WRAP_CONTENT
 机器人:layout_width =FILL_PARENT
 机器人:方向=垂直>< Horizo​​ntalScrollView
 机器人:layout_height =WRAP_CONTENT
 机器人:layout_width =WRAP_CONTENT
 机器人:滚动条=无
 机器人:fillViewport =真正的>< TabWidget机器人:ID =@机器人:ID /标签
 机器人:layout_height =40dp
 机器人:layout_width =WRAP_CONTENT
 机器人:layout_gravity =底
 机器人:重力=底>< / TabWidget>
< / Horizo​​ntalScrollView><的FrameLayout
 机器人:ID =@机器人:ID / tabcontent
 机器人:layout_height =FILL_PARENT
 机器人:layout_width =FILL_PARENT>
< /&的FrameLayout GT;
< / LinearLayout中>
< /滚动型>
< / TabHost>

SaleExecutiveDashboard.java

 包com.androidpeople.tab;进口android.app.TabActivity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.widget.TabHost;
进口android.widget.TabHost.TabSpec;公共类SalesExecutiveDashboard扩展TabActivity {
    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.tab);        / * TabHost将有标签* /
        // TabHost tabHost =(TabHost)findViewById(android.R.id.tabhost);        TabHost tabHost = getTabHost();        / *则tabspec用于创建一个新的标签。
         *通过使用则tabspec只有我们能够setContent的标签。
         *通过使用则tabspec setIndicator(),我们可以设置名称标签。 * /        / * TID1是firstTabSpec标识。它习惯了在外面访问。 * /
        则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()被用于为标签集名称。 * /
        / *则tabspec setContent()被用于设置特定标签的内容。 * /
        HomeTabSpec.setIndicator(家).setContent(新意图(这一点,HomeTab.class));        RevExpTabSpec.setIndicator(转/ EXP).setContent(新意图(这一点,RevExpTab.class));
        AccountTabSpec.setIndicator(帐户).setContent(新意图(这一点,AccountsTab.class));
        DistroTabSpec.setIndicator(发行).setContent(新意图(这一点,DistroTab.class));
        SBPTabSpec.setIndicator(按产品销售额).setContent(新意图(这一点,SBPTab.class));
        AlertTabSpec.setIndicator(通知).setContent(新意图(这一点,AlertTab.class));        / *加入则tabspec到TabHost显示。 * /
        tabHost.addTab(HomeTabSpec);
        tabHost.addTab(RevExpTabSpec);
        tabHost.addTab(AccountTabSpec);
        tabHost.addTab(DistroTabSpec);
        tabHost.addTab(SBPTabSpec);
        tabHost.addTab(AlertTabSpec);    }
}

HomeTab.java

 包com.androidpeople.tab;进口android.app.Activity;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.graphics.Matrix;
进口android.os.Bundle;
进口android.widget.ImageView;公共类HomeTab延伸活动{
    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);        / *第一个选项卡的内容* /
        ImageView的图像=(ImageView的)findViewById(R.id.pie_chart);        // TextView中的TextView =新的TextView(本);
        //textView.setText(\"This是主页选项卡);
        //的setContentView(TextView的);
        的setContentView(R.layout.main);    }
}


解决方案

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

  //握住你的形象的整数资源ID
整数IMG = R.drawable&所述;&下; imageid>取代;//得到您的ImageView
ImageView的的iView =(ImageView的)findViewById(R.id.<<&视图名GT;>);//指定图像
iView.setImageResource(IMG);

另一个选择是在绘制目录,也可以创建XML'选择',告诉您的应用程序使用的图像依赖于某些条件。例如,假设你有一个ImageButton的,并希望不同的图像显示,如果它的禁用。你可以像在绘制目录下面的一个选择你的绘制-H / L / MDPI目录使用的图像:

 <?XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=htt​​p://schemas.android.com/aps/res/android>
    <项目的android:state_enabled =false的机器人:可绘制=@绘制/<< disabledImage>>/>
    <项目机器人:可绘制=@绘制/<< defaultImage>>>
< /选择>

您可以然后在布局中使用这个选择。

 <按钮的android:layout_width =FILL_PARENT机器人:ID =@ + ID / BTN1机器人:文本=BTN1机器人:TEXTSIZE =20dp机器人:文字颜色=@机器人:彩色/透明的机器人:背景=@绘制/<< selectorName>>中机器人:layout_height =FILL_PARENT安卓的onClick =<< clickevent>>中/>

我知道这是一个老问题,但希望这可以帮助别人。

My friend and I are making an android app for our class project. We are making and app with tabs. We want to know if there is a way to use one xml file for the tabs and if we can add pictures straight from the java code or do we need to make an xml file for each individual tab. If we do how would we go about keeping the same formatting for all the tabs.

The code i provide is the main.xml in which we have attached some pictures to. the tab.xml is the xml layout which we formatted the tabs. the SalesExecutiveDashboard.java is the main activity that calls the other tab activities. Lastly HomeTab.java is and example of on our tab codes.

Thanks for all the help provided

main.xml

<?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

<?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

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

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);

    }
}

解决方案

Yes, you can use one layout resource multiple times. It only specifies the appearance. As for the images, so long as they are in your drawable resources, you would just assign them to your ImageView as you wished.

//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);

Another option is in the drawable directory, you can create xml 'selectors' to tell your app which image to use dependent on some condition. For example, let's say you have an ImageButton and want a different image to show if it's disabled. You can have a selector like the following in the drawable directory to use the images in your drawable-h/l/mdpi directories:

<?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>

You could then use this selector in your layout.

<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>>" />

I know this is an old question, but hope this helps somebody.

这篇关于你怎么能在Java code图像添加到Android应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆