[英]How can you retreive images from firebase and display in gridview android
[英]How can you add images to Android App from java code
我和我的朋友正在為我們的課堂項目制作一個android應用。 我們正在制作帶有標簽的應用程序。 我們想知道是否有一種方法可以為標簽使用一個xml文件,並且是否可以直接從Java代碼添加圖片,還是需要為每個標簽創建一個xml文件。 如果這樣做,我們將為所有標簽保持相同的格式。
我提供的代碼是main.xml,我們在其中附加了一些圖片。 tab.xml是我們格式化選項卡的xml布局。 SalesExecutiveDashboard.java是調用其他選項卡活動的主要活動。 最后,HomeTab.java是我們的選項卡代碼上的示例。
感謝您提供的所有幫助
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);
}
}
是的,您可以多次使用一種布局資源。 它僅指定外觀。 至於圖像,只要它們在您的可繪制資源中,您就可以根據需要將它們分配給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);
另一個選項是在drawable目錄中,您可以創建xml'選擇器'來告訴您的應用根據某些條件使用哪個圖像。 例如,假設您有一個ImageButton並希望顯示其他圖像(如果已禁用)。 您可以在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>
然后,您可以在布局中使用此選擇器。
<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>>" />
我知道這是一個古老的問題,但是希望這對某人有幫助。
首先,您需要編輯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); }
我們可以在設置了您不能先投射的布局文件后投射任何控件:)
然后在您的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(); } });
您可以在子活動中使用此SharedPreferences值並檢查位置條件以在一種布局中或僅在一個Java文件中設置動態圖像
我希望它能工作
快樂編碼...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.