- 浏览: 2551052 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Android UI(2)Getting Started - Supporting Devices and Fragments
3. Supporting Different Devices
Supporting Different Languages
Keep the UI String to external file instead of in the Java Codes.
Create Locale Directories and String Files
Multiple languages
res/
values/
strings.xml
values-es/
strings.xml
values-ft/
strings.xml
Use the String Resources
In Java Codes
The syntax to refer to a string resource is R.string.<string_name>
In XML files
@string/<string_name>
Supporting Different Screens
4 sizes: small, normal, large, large
4 densities: low(LDPI), medium(MDPI), high(HDPI), extra high(XHDPI)
Create Different Layouts
res/
layout/
main.xml
layout-large/
main.xml
The system will reference the layout file as usual
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
Create Different Bitmaps
@drawable/awesomeimage will reference to the bitmap.
Supporting Different Platform Versions
…snip…
4. Building a Dynamic UI with Fragments
Download the sample project FragmentBasics.zip
Using the Support Library
Set Up Your Project with the Support Library
Make sure I download the Extras/Android Support
Make sure I copy the jar from /opt/android-sdk/extras/android/support/v4/android-support-v4.jar to libs under my own project.
And change my configuration on Manifest file to
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="17"/>
Import the Support Library APIs
…snip…
Creating a Fragment
A fragment is a muddler section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running. (sub activity which you can reuse in different activities)
Create a Fragment Class
Just extends the Fragment class.
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.ViewGroup;
public class ArticleFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.article_view,container, false);
}
}
The lifecycle of Fragment is as like as Activity.
Add a Fragment to an Activity using XML
FragmentActivity is a special activity provided in the Support Library to handle fragments on system versions older than API level 11. After version 11, we can use a regular Activity.
<LinearLayout …>
<fragment android:name="com.example.android.fragments.HeadlinesFragment"
android:id="@+id/headlines_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
Building a Flexible UI
The FragmentManager class provides methods that allow you to add, remove, and replace fragments to an activity at runtime.
Add a Fragment to an Activity at Runtime
We only define an empty container here
<FrameLayout …snip…
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Inside my activity, call getSupportFragmentManager() to get a FragmentManager using the Support Library APIs. Then call beginTransaction() to create a FragmentTransaction and call add() to add a fragment.
Commit the changes with commit().
HeadlinesFragment firstFragment = new HeadlinesFragment();
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, firstFragment).commit();
Replace One Fragment with Another
Use the method replace() instead of add().
ArticleFragment newFragment = new ArticleFragment();
Bundle args = new Bundle();
args.putInt(ArticleFragment.ARG_POSITION, position); //just remember which article we choose
newFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null); //make the back button working
transaction.commit();
Communicating with Other Fragments
All Fragment-to-Fragment communication is done through the associated Activity. 2 Fragments should never communicate directly.
Define an Interface
We define an interface and callback method, implement in Activity.
public class HeadlinesFragment extends ListFragment {
OnHeadlineSelectedListener mCallback;
public interface OnHeadlineSelectedListener {
public void onArticleSelected(int position);
}
public void onAttach(Activity activity) {
super.onAttach(activity);
try{
mCallback = (OnHeadlineSelectedListener)activity;
}catch(ClassCastException e){
throw new ClassCastException(activity.toString() + " must implement OnHeadlineSelectedListener");
}
}
}
Send the event to Activity
public void onListItemClick(ListView l, View v, int position, long id) {
mCallback.onArticleSelected(position);
}
The fragment bind the interface to Activity, send the event.
The activity will implement the interface.
Deliver a Message to a Fragment
If the fragment is there, directly call the method.
ArticleFragment articleFrag = (ArticleFragment) getSupportFragmentManager().findFragmentById(R.id.articles_fragment);
if(articleFrag != null){
articleFrag.updateArticleView(position);
}else{
…snip...
}
References:
http://developer.android.com/training/basics/supporting-devices/index.html
http://developer.android.com/training/basics/fragments/creating.html
http://developer.android.com/training/basics/fragments/fragment-ui.html
3. Supporting Different Devices
Supporting Different Languages
Keep the UI String to external file instead of in the Java Codes.
Create Locale Directories and String Files
Multiple languages
res/
values/
strings.xml
values-es/
strings.xml
values-ft/
strings.xml
Use the String Resources
In Java Codes
The syntax to refer to a string resource is R.string.<string_name>
In XML files
@string/<string_name>
Supporting Different Screens
4 sizes: small, normal, large, large
4 densities: low(LDPI), medium(MDPI), high(HDPI), extra high(XHDPI)
Create Different Layouts
res/
layout/
main.xml
layout-large/
main.xml
The system will reference the layout file as usual
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
Create Different Bitmaps
@drawable/awesomeimage will reference to the bitmap.
Supporting Different Platform Versions
…snip…
4. Building a Dynamic UI with Fragments
Download the sample project FragmentBasics.zip
Using the Support Library
Set Up Your Project with the Support Library
Make sure I download the Extras/Android Support
Make sure I copy the jar from /opt/android-sdk/extras/android/support/v4/android-support-v4.jar to libs under my own project.
And change my configuration on Manifest file to
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="17"/>
Import the Support Library APIs
…snip…
Creating a Fragment
A fragment is a muddler section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running. (sub activity which you can reuse in different activities)
Create a Fragment Class
Just extends the Fragment class.
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.ViewGroup;
public class ArticleFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.article_view,container, false);
}
}
The lifecycle of Fragment is as like as Activity.
Add a Fragment to an Activity using XML
FragmentActivity is a special activity provided in the Support Library to handle fragments on system versions older than API level 11. After version 11, we can use a regular Activity.
<LinearLayout …>
<fragment android:name="com.example.android.fragments.HeadlinesFragment"
android:id="@+id/headlines_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
Building a Flexible UI
The FragmentManager class provides methods that allow you to add, remove, and replace fragments to an activity at runtime.
Add a Fragment to an Activity at Runtime
We only define an empty container here
<FrameLayout …snip…
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Inside my activity, call getSupportFragmentManager() to get a FragmentManager using the Support Library APIs. Then call beginTransaction() to create a FragmentTransaction and call add() to add a fragment.
Commit the changes with commit().
HeadlinesFragment firstFragment = new HeadlinesFragment();
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, firstFragment).commit();
Replace One Fragment with Another
Use the method replace() instead of add().
ArticleFragment newFragment = new ArticleFragment();
Bundle args = new Bundle();
args.putInt(ArticleFragment.ARG_POSITION, position); //just remember which article we choose
newFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null); //make the back button working
transaction.commit();
Communicating with Other Fragments
All Fragment-to-Fragment communication is done through the associated Activity. 2 Fragments should never communicate directly.
Define an Interface
We define an interface and callback method, implement in Activity.
public class HeadlinesFragment extends ListFragment {
OnHeadlineSelectedListener mCallback;
public interface OnHeadlineSelectedListener {
public void onArticleSelected(int position);
}
public void onAttach(Activity activity) {
super.onAttach(activity);
try{
mCallback = (OnHeadlineSelectedListener)activity;
}catch(ClassCastException e){
throw new ClassCastException(activity.toString() + " must implement OnHeadlineSelectedListener");
}
}
}
Send the event to Activity
public void onListItemClick(ListView l, View v, int position, long id) {
mCallback.onArticleSelected(position);
}
The fragment bind the interface to Activity, send the event.
The activity will implement the interface.
Deliver a Message to a Fragment
If the fragment is there, directly call the method.
ArticleFragment articleFrag = (ArticleFragment) getSupportFragmentManager().findFragmentById(R.id.articles_fragment);
if(articleFrag != null){
articleFrag.updateArticleView(position);
}else{
…snip...
}
References:
http://developer.android.com/training/basics/supporting-devices/index.html
http://developer.android.com/training/basics/fragments/creating.html
http://developer.android.com/training/basics/fragments/fragment-ui.html
发表评论
-
ionic UI(4)ionic2 framework - basic and components and native
2016-03-24 02:33 1261ionic UI(4)ionic2 framework - b ... -
ionic UI(3)TypeScript - handbook
2016-03-22 23:21 634ionic UI(3)TypeScript - handboo ... -
ionic UI(2)ionic2 framework - TypeScript - tutorial
2016-03-22 06:52 1656ionic UI(2)ionic2 framework - T ... -
Parse and Heroku Service(3)Parse Server and Parse Dashboard
2016-03-22 06:30 967Parse and Heroku Service(3)Pars ... -
Parse and Heroku Service(2)Mail Templates and Push Notification
2016-03-22 02:45 583Parse and Heroku Service(2)Mail ... -
ionic UI(1)Introduction
2016-03-19 03:18 723ionic UI(1)Introduction 1 Inst ... -
Parse and Heroku Service(1)Heroku Installation and Play
2016-03-19 00:13 824Parse and Heroic Service(1)Hero ... -
Hybrid(5)Customize Meteor Directly Google Login
2015-09-01 02:33 910Hybrid(5)Customize Meteor Direc ... -
Hybrid(4)Favorite Places - Google Login
2015-09-01 02:02 1341Hybrid(4)Favorite Places - Goog ... -
Hybrid(3)More Meteor Example - Social
2015-08-11 05:04 756Hybrid(3)More Meteor Example - ... -
Hybrid(2)meteor Running Android and iOS
2015-07-28 23:59 1049Hybrid(2)meteor Running Android ... -
Create the Google Play Account
2015-07-18 06:42 1101Create the Google Play Account ... -
Secure REST API and Mobile(1)Document Read and Understand OAUTH2
2015-07-14 00:36 764Secure REST API and Mobile(1)Do ... -
Screen Size and Web Design
2015-07-11 01:11 725Screen Size and Web Design iPh ... -
Hybrid(1)ionic Cordova meteor
2015-06-25 05:49 471Hybrid(1)ionic Cordova meteor ... -
Android Fire Project(1)Recall Env and Knowledge
2015-02-11 12:28 686Android Fire Project(1)Recall ... -
Android Content Framework(1)Concept
2014-06-14 13:54 1080Android Content Framework(1)Con ... -
Feel Android Studio(1)Install and Update Android Studio
2014-04-11 03:12 2029Feel Android Studio(1)Install a ... -
IOS7 App Development Essentials(2)iBeacon
2014-03-05 05:55 890IOS7 App Development Essentials ... -
IOS7 App Development Essentials(1) Persistent Store
2014-03-05 05:54 1327IOS7 App Development Essentials ...
相关推荐
android-material-drawer-template NEW I released a new template. You can install it on your Android Studio to create Material Design applications easier than ever. Check it out here: ...
The code in this document augments TPM 2.0 Part 2 and TPM 2.0 Part 3 to provide a complete description of a TPM, including the supporting framework for the code that performs the command actions. ...
Featuring the Latest Windows 8.1 ReleaseSUPPORTING WINDOWS 8 is an essential resource for current and aspiring PC repair technicians who need to know up-to-date information on how to support the ...
在开发Android应用时,考虑不同平台版本的支持是至关重要的。Android设备具有多种屏幕尺寸和密度,为了确保应用能够在尽可能多的设备上良好运行,开发者需要考虑为不同屏幕尺寸和密度准备相应的资源。 Android系统...
多语言支持是Android应用开发中的一个重要方面,它允许应用根据不同国家和地区的语言环境提供相应的本地化资源。这不仅包括文本内容的翻译,还可能涉及日期、数字格式以及资源文件(如图片和声音)的本地化。为了...
Zim is a great offline notetaking software that supports LaTeX equations. The running speed is fairly fast.
The Busy Coder’s Guide to Android Development covers a wide range of Android capabilities and APIs, from creating simple user interfaces, to supporting long-running background processes, through the...
wireless devices that reutilize the cellular spectrum and radio interface. The second is that of Machine-Type Communications (MTC), where the objective is to attach a large number of lowrate low-power...
This library provides a highly configurable logging framework for Android apps, supporting multiple log destinations simultaneously: files SQLite databases logcat sockets syslog email Runs on ...
TPM-Rev-2.0-Part-2-Structures-01.38 TPM-Rev-2.0-Part-3-Commands-01.38 TPM-Rev-2.0-Part-3-Commands-01.38-code TPM-Rev-2.0-Part-4-Supporting-Routines-01.38 TPM-Rev-2.0-Part-4-Supporting-Routines-01.38-...
Android and Projects Tutorial #2 - Creating a Stub Project Getting Around Android Studio Contents of Android Projects Introducing Gradle and the Manifest Tutorial #3 - Manifest Changes Some Words ...
精准肿瘤治疗,即精准肿瘤学(Precision Oncology),是一种基于每个患者个体化的癌症治疗方法,其核心在于针对特定的生物过程、遗传突变进行治疗,或者精确增强个体的免疫系统。精准肿瘤治疗始于20世纪70年代,当时...
PowerCenter Getting Started is written for the developers and software engineers who are responsible for implementing a data warehouse. It provides a tutorial to help first-time users learn how to use...
Android Text Samples These samples show how to work with text in Android. Explore the samples The TextStyling Java and Kotlin samples show how to style text using spans. The RoundedBackground sample ...
Get started quickly with SwiftUI and Combine reactive programming framework through practice, and master the next generation client UI development technology At WWDC 2019, Apple announced the ...
(Currently supporting devices upto api 9 ) Material Preference Library uses com.android.support:preference-v7:x.x.x support library widgets. Also it includes a color chooser dialog widget, that can ...
"pybamm-supporting-material:PyBaMM的支持材料"这个压缩包包含了PyBaMM框架的相关辅助资料,如幻灯片、海报和论文,为学习和理解PyBaMM提供了一个全面的资源库。 首先,我们来看看这个压缩包中的核心内容。...