简体   繁体   English

如何从Java代码向Android App添加图像

[英]How can you add images to Android App from java code

My friend and I are making an android app for our class project. 我和我的朋友正在为我们的课堂项目制作一个android应用。 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. 我们想知道是否有一种方法可以为标签使用一个xml文件,并且是否可以直接从Java代码添加图片,还是需要为每个标签创建一个xml文件。 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. 我提供的代码是main.xml,我们在其中附加了一些图片。 the tab.xml is the xml layout which we formatted the tabs. tab.xml是我们格式化选项卡的xml布局。 the SalesExecutiveDashboard.java is the main activity that calls the other tab activities. SalesExecutiveDashboard.java是调用其他选项卡活动的主要活动。 Lastly HomeTab.java is and example of on our tab codes. 最后,HomeTab.java是我们的选项卡代码上的示例。

Thanks for all the help provided 感谢您提供的所有帮助

main.xml 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 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 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 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. 至于图像,只要它们在您的可绘制资源中,您就可以根据需要将它们分配给ImageView。

//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. 另一个选项是在drawable目录中,您可以创建xml'选择器'来告诉您的应用根据某些条件使用哪个图像。 For example, let's say you have an ImageButton and want a different image to show if it's disabled. 例如,假设您有一个ImageButton并希望显示其他图像(如果已禁用)。 You can have a selector like the following in the drawable directory to use the images in your drawable-h/l/mdpi directories: 您可以在drawable目录中具有如下选择器,以使用drawable-h / l / mdpi目录中的图像:

<?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. 我知道这是一个古老的问题,但是希望这对某人有帮助。

First of all you need to edit your HomeTab Activity onCreate method like : 首先,您需要编辑HomeTab Activity onCreate方法,例如:

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

We can casting any controll after set it layout file you cant casting first :) 我们可以在设置了您不能先投射的布局文件后投射任何控件:)

Then after your MainActivity content TabHost so you can set it inBuild Listner like : 然后在您的MainActivity内容TabHost之后,可以像下面这样在Build Listner中进行设置:

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

you can use this SharedPreferences value in your sub Activity and check position condition to set dynamic image in your one layout or only in one Java file 您可以在子活动中使用此SharedPreferences值并检查位置条件以在一种布局中或仅在一个Java文件中设置动态图像

i hope it work 我希望它能工作

Happy Coding ... 快乐编码...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Firebase中检索图像并显示在GridView Android中 - How can you retreive images from firebase and display in gridview android Java:如何将图像从文件添加到ResourceBundle? - Java: How can I add images from a file into a ResourceBundle? 如何从我的 Android 应用程序中的 Java 代码在我的布局上添加 NumberPicker? - How can I add NumberPicker on my Layout from Java code in my Android application? 如何从 Kotlin 中的 Android 应用程序发送文本和图像? - How can I tweet texts and images from an Android App in Kotlin? 如何从Android应用程序内部发送网络请求? - How can you send a web request from inside an Android app? Android 应用程序:如何用 java 代码打印出一个字符串? - Android App: How can I print out a String with java code? 在Java版本的Google App Engine中,您如何评估和执行以字符串形式传入的Java代码和单元测试? - In the Java version of Google App Engine, how can you eval and execute Java code and unit tests passed in as strings? 如何在Java文件中添加图像? - How can I add images in a java file? 你能写一个应用程序来记录 Android Java 中特定应用程序的屏幕吗? - Can you write an app that records the screen of a specific app in Android Java? 如何使用 itext7 Java 将多个图像添加到 PDF? - How do you add multiple images to a PDF with itext7 Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM