Android学习(6)-UI介绍

来源:互联网 发布:php curl get 数据 编辑:程序博客网 时间:2024/05/29 13:36

界面设计和功能开发同样重要,界面美观的应用程序可以大大增加用户粘性,还能帮助我们吸引到更多的新用户。为了以后做出比较复杂的界面,应该以XML为基础编写界面。把界面化定制界面看成是一个预览工具。

(1) TextView

<TextView 

android: id="@+id/text_view"

android:layout_width="match_parent"

android:layout_height="wrap_content"   //宽度和高度:match_parent  fill_parent,这两个差不多,推荐第一个。 wrap_content表示控件大小取决于包含内容的大小。

android:gravity="center"  //字体位置

android:textSize="24sp"

android:textColor="#00ff00"

android:text="This is textView"

/>


(2) Button

<Button

android:id="@+id/button"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Button"

/>

在onCreate中可以通过以下代码设置按钮事件:

Button button = (Button)findViewById(R.id.button);

button.setOnClickListener(new OnClickListener({

public void onClick(View v){

}

})

);

以上是通过匿名类的方式注册监听器,也可以使用实现接口来做。

public class SomeActivity extends Activity implements OnClickListener{

public void onClick(View v){

switch(v.getId()){
case R.id.button:

......

}

}

}


(3) EditText

<EditText

android:id="@+id/edit_text"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Type something here" //文本框提示内容

android:maxLines="2"  //文本框可显示最大行数

/>

代码中获取文本框输入内容:

EditText editText = (EditText)findViewById(R.id.edit_text);

String inputText = editText.getText().toString();


(4) ImageView

<ImageView

android:id="@+id/image_view"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:src="@drawable/ic_launcher"

/>

在代码中修改图片:

ImageView imageView = (ImageView)findViewById(R.id.image_view);

imageView.setImageResource(R.drawable.jelly_bean);


(5) ProgressBar

<ProgressBar

android:id="@+id/progress_bar"

android:layout_width="match_parent"

android:layout_height="wrap_content"

/>

设置进度条可见不可见:

private ProgressBar progressBar;

protected void onCreate(){

progressBar=(ProgressBar)findViewById(R.id.progress_bar);

button.setOnClickListener(this);

}

public void onClick{

if(progressBar.getVisibility() == View.GONE)

progressBar.setVisibility(View.VISIBLE);

}

以上进度条是旋转型圆圈,可以改成是水平进度条。设置为:

style="?android:attr/progressBarStyleHorizontal"

android:max="100"

可以通过代码改变其进度:

int progress = progressBar.getProgress();

progress += 10;

progressBar.setProgress(progress);


(6) AlertDialog

alertdialog用于提示一些非常重要的内容或者警告信息。

AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);

dialog.setTitle("...");

dialog.setMessage("...");

dialog.setCancleable(false);

dialog.setPositiveButton("OK",new DialogInterface.OnClickListener(){

public void onClick(...){...}

});

(7) ProgressDialog

弹出对话框中的是进度条。

ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);

progressDialog.setTitle(...);

progressDialog.setMessage(...);

progressDialog.setCancelable(true);

progressDialog.show();


(8)px  pt  dp sp

px和pt是像素和磅值,用于pc时候限制大小和文字等不错,但是手机屏幕小,影响就比较大。

dp sp是谷歌提出的用于手机上的单位,以单位密度为准,比较好用。



0 0
原创粉丝点击