Android学习之散乱的知识点

1.

安卓广告知名平台:有米,哇棒,架势,admob(国外,但效果不好)等,推荐用有米

2.

src目录为源文件目录,所有可以被用户修改和创建的Java文件将被存放在这个目录下

3.

xml中引用strings.xml

"@string/string_name"

在java文件中用

Activity.this.getString(R.string.resource_name) 

4.

内容提供者主要用于不同程序之间进行数据共享。

5.

最好用onPause()方法来保存在停止与用户交互前更改的数据

6.

padding属性通常用来描述控件里面的内容与控件的关系

layout_margin通常用来描述控件之间的位置关系

7.

gravity用于设置该控件里的对齐方法

layout_gravity 用于设置控件 在父控件中的对齐方式

8.

最好用sp来描述文字

其他用dp

9.

  res/drawable/myimage.png位置保存了一张图片,在Layout XML中可以应用这个图片到一个View上:

    <ImageView

        android:layout_height="wrap_content"

        android:layout_width="wrap_content"

        android:src="@drawable/myimage" />

 

    下面的代码可以以Drawable方式得到图片:

    Resources res = getResources();

    Drawable drawable = res.getDrawable(R.drawable.myimage);

10.

id值。

11.

Textview在多行情况下如何显示省略号。

http://blog.sina.com.cn/s/blog_5eea5ce00101cugf.html

 

12.

<merge/>用法

http://www.cnblogs.com/travelfromandroid/articles/2133206.html 

13.

 

在该Activity的onCreate函数中添加控制代码:

 

               this.requestWindowFeature(Window.FEATURE_NO_TITLE);  //设置Activity标题不显示
               this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);  //设置全屏显示

 

       注意:这两句代码必须写在setContentView函数的前面,否则运行会报错。

14.

能用内置的适配器,就用内置的适配器,这样只需写好子布局就可以了。

//用Map

 

List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
int length = itemName.length;
for (int i = 0; i < length; i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("ItemImageView", imageRes[i]);
map.put("ItemTextView", itemName[i]);
data.add(map);
}
//为itme.xml添加适配器
SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this,
data, R.layout.gv, new String[] { "ItemImageView","ItemTextView" }, new int[] { R.id.ItemImageView,R.id.ItemTextView });
grid.setAdapter(simpleAdapter);

15.

上下文要是Acitivity的话,可用getApplicationContext()来获得返回值

16.

Toast用法

简单版:

Toast toast = Toast.makeText(ToastDemoActivity.this, "这是一个普通的Toast!", Toast.LENGTH_SHORT); 

//三个参数:上下文,显示字符串,时间(单位为ms)
toast.show(); 

//记得展现出来。

设置位置:

 方法一:setGravity(int gravity, int xOffset, int yOffset)三个参数分别表示(起点位置,水平向右位移,垂直向下位移)

 例子:.setGravity(Gravity.CENTER, 0, 0);

 例子:.setGravity(Gravity.TOP | Gravity.LEFT, 0, 200); 


  方法二:setMargin(float horizontalMargin, float verticalMargin)
  以横向和纵向的百分比设置显示位置,参数均为float类型(水平位移正右负左,竖直位移正上负下)

 例子:.setMargin(-0.5f, 0f); 

 

自定义版本:

http://blog.csdn.net/coolszy/article/details/6365825

1.添加一个布局文件,该布局文件的结构和Activity使用的布局文件结构一致(可以不一致),在该布局文件中我们需设计我们Toast的布局。

2.在这个地方要注意,我们给LinearLayout添加的id属性,在后面的把xml转换为view代码中我们需要使用到。在程序中,我们可以通过如下代码创建我们自己的Toast。

  1. 获取LayoutInflater对象,该对象能把XML文件转换为与之一直的View对象  
  2.                 LayoutInflater inflater = getLayoutInflater();  
  3.                 //根据指定的布局文件创建一个具有层级关系的View对象  
  4.                 //第二个参数为View对象的根节点,即LinearLayout的ID  
  5.                 View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));  
  6.                   
  7.                 //查找ImageView控件  
  8.                 //注意是在layout中查找  
  9.                 ImageView image = (ImageView) layout.findViewById(R.id.image);  
  10.                 image.setImageResource(R.drawable.head);  
  11.                 TextView text = (TextView) layout.findViewById(R.id.text);  
  12.                 text.setText("自定义Toast演示程序");  
  13.   
  14.                 Toast toast = new Toast(getApplicationContext());  
  15.                 //设置Toast的位置  
  16.                 toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);  
  17.                 toast.setDuration(Toast.LENGTH_LONG);  
  18.                 //让Toast显示为我们自定义的样子  
  19.                 toast.setView(layout);  //重要之处
  20.                 toast.show();  

17.

LinearLayout不能设置圆角背景。

18.

圆角:

设置边框圆角可以在drawable-mdpi目录里定义一个xml: 

例:corner.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#DAAA" />
<corners android:topLeftRadius="10dp"
android:topRightRadius="10dp"
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"/>

</shape>

 

如果想引用这个xml,只需要@drawable/corners_bg.xml即可: 

android:background="@drawable/corner" 

 

例子:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/toast_layout_root"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:padding="15dip" android:layout_margin="15dip"
android:background="@drawable/corner"
>

<ImageView android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginRight="10dp"
/>


<TextView android:id="@+id/text"
android:layout_width="100dp"
android:layout_height="100dp"
android:textColor="#FFF"
android:gravity="center"
android:layout_toRightOf="@id/image"

/>

</RelativeLayout>

 19.

布局大小控制技巧

android:layout_height="0dp" //或者宽度
android:layout_weight="1" //表示剩余布局的分配

20. web的使用。

xml中:

<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>

Java中:

webView=(WebView) findViewById(R.id.webView);
webView.loadUrl("Http://www.baidu.com");

要使网页真正加载在布局中,需要调用

webView.setWebViewClient(new WebViewClient(){

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO 自动生成的方法存
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
});

更多方法:

webView.setWebChromeClient(new WebChromeClient(){
@Override
public void onReceivedTitle(WebView view, String title) {
// title为网页的标题
super.onReceivedTitle(view, title);
}
});

使用webView实现下载;

webView.setDownloadListener(new DownloadListener() {
            public void onDownloadStart(String url, String userAgent,
                            String contentDisposition, String mimetype,
                            long contentLength) {
                    //实现下载的代码
                                          Uri uri = Uri.parse(url);
           Intent intent = new Intent(Intent.ACTION_VIEW,uri);
                    startActivity(intent);
            }
    });

如果访问的页面中有Javascript,则webview必须设置支持Javascript

webview.getSettings().setJavaScriptEnabled(true);

设置WevView要显示的网页:

 互联网用:webView.loadUrl("http://www.google.com"); 
 本地文件用:webView.loadUrl("file:///android_asset/XX.html");  本地文件存放在:assets文件中.可以加载404网页

 另外还有其他一些可重写的方法 
1,接收到Http请求的事件
onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) 

 

2,打开链接前的事件
public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } 

这个函数我们可以做很多操作,比如我们读取到某些特殊的URL,于是就可以不打开地址,取消这个操作,进行预先定义的其他操作,这对一个程序是非常必要的。
 
3,载入页面完成的事件
public void onPageFinished(WebView view, String url){ } 

同样道理,我们知道一个页面载入完成,于是我们可以关闭loading条,切换程序动作。
 
4,载入页面开始的事件
public void onPageStarted(WebView view, String url, Bitmap favicon) { } 

这个事件就是开始载入页面调用的,通常我们可以在这设定一个loading的页面,告诉用户程序在等待网络响应。
 
通过这几个事件,我们可以很轻松的控制程序操作,一边用着浏览器显示内容,一边监控着用户操作实现我们需要的各种显示方式,同时可以防止用户产生误操作。

 

按back返回上一个网页,重写Activity中的onKeyDown方法:

public boolean onKeyDown(int keyCoder,KeyEvent event){
if(webView.canGoBack() && keyCoder == KeyEvent.KEYCODE_BACK){
webview.goBack(); //goBack()表示返回webView的上一页面

return true;
}
return false;
}

 

使用内置浏览器加载网页:

  1.   Uri uri = Uri.parse(url); //url为你要链接的地址  
  2.   
  3.               Intent intent =new Intent(Intent.ACTION_VIEW, uri);
  4.             startActivity(intent);  

Webview与js交互:需要js方面的知识

 mWebView.goBack();   //后退  
mWebView.goForward();//前进

mWebView.reload();  //刷新

 

21.

比较常用就记录下来了

bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="58dp"
android:background="@drawable/bottom_bar"
android:orientation="horizontal"


>

<LinearLayout
android:id="@+id/id_tab1"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
>

<ImageButton
android:layout_marginTop="5dp"
android:id="@+id/id_btn1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/return_img"
android:background="#00000000"
android:clickable="false"
/>

</LinearLayout>
<LinearLayout
android:id="@+id/id_tab2"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_weight="3"
android:gravity="center"
android:orientation="vertical"
>

<TextView
android:id="@+id/tvTitle"
android:text="Home"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#ffffff"
/>
</LinearLayout>

<LinearLayout
android:id="@+id/id_tab3"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
>

<ImageButton
android:layout_marginTop="5dp"
android:id="@+id/id_btn3"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/refresh_img"
android:background="#00000000"
android:clickable="false"
/>

</LinearLayout>
</LinearLayout>

引用:<include layout="@layout/bottom"/>

22.

top.xml

<?xml version="1.0" encoding="utf-8"?>
<RelateLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="40dp"

android:background="@drawable/title_bg"
>

<ImageButton

android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:src="@drawable/return_img"

/>

<TextView

android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="R"
android:textColor="#ffffff"
android:textSize="20sp"
android:textStyle="bold"
android:gravity="center"
android:layout_gravity="center"
android:layout_weight="1"
/>

</RelateLayout>

引用:<include layout="@layout/top"/>

 23.

自定义PogresBar进度条(网页进度条)

主布局文件中:

<ProgressBar
android:id="@+id/pb"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="3dip"
android:indeterminateOnly="false"
android:max="100"
android:progressDrawable="@drawable/progress_bar_states" >
</ProgressBar>

接下来就是progress_bar_states

各种状态的设置

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@android:id/background">
<shape>
<gradient
android:startColor="#ffffff"
android:centerColor="#ffffff"
android:endColor="#ffffff"

/>
</shape>
</item>

<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<gradient
android:startColor="#234"
android:centerColor="#234"
android:endColor="#a24"
/>
</shape>
</clip>
</item>

<item android:id="@android:id/progress">
<clip>
<shape>
<gradient
android:startColor="#33000001"
android:centerColor="#40000000"
android:endColor="#44000000"
/>
</shape>
</clip>
</item>

</layer-list>

24.

posted @ 2015-04-09 23:06  BB的想法  阅读(213)  评论(0编辑  收藏  举报