Android Studio 点击按钮跳转新界面

Android Studio 点击按钮跳转新界面
问题描述
首先,我们有两个Java文件和与之绑定的xml文件。此处以HistoryActivity.java,activity_history.xml 和 EventDetail.java,activity_event_detail.xml为例。我们要实现在HistoryActivity界面中添加一个按钮,并且点击跳转到EventDetail界面。

为HistoryActivity界面添加按钮
在其对应的activity_history.xml 中:

<?xml version=”1.0″ encoding=”utf-8″?>
<android.support.constraint.ConstraintLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:app=”http://schemas.android.com/apk/res-auto”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.HistoryActivity”>

<Button
android:id=”@+id/History”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Historical Event”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true”/>
</android.support.constraint.ConstraintLayout>

我们通过android:id=”@+id/History”语句讲button的id设置为History,在之后设置点击事件时使用。

为History按钮添加点击事件
在HistoryActivity.java中:

package com.example.xff.tm;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.widget.Button;
import android.widget.*;

public class HistoryActivity extends AppCompatActivity {
Button button = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
button = (Button)findViewById(R.id.History);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(HistoryActivity.this,EventDetail.class);
startActivity(intent);
}
});
}

}

通过之前定义的button的id来找到对应button,为之设置点击监听。当发生点击事件时,通过Intent进行跳转。
#在manifests->AndroidManifest.xml中添加activity(这个步骤通常是添加点击事件之后系统自动生成,可以进行检查)

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.xff.tm”>

<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:roundIcon=”@mipmap/ic_launcher_round”
android:supportsRtl=”true”
android:theme=”@style/AppTheme”>
<activity android:name=”.HistoryActivity”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity android:name=”.EventDetail”></activity>
</application>

</manifest>

EventDetail.java,activity_event_detail.xml
作为被跳转的界面,这两个文件只需要完成自己的功能即可:
EventDetail.java:

package com.example.xff.tm;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class EventDetail extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_event_detail);
}
}

activity_event_detail.xml:

<?xml version=”1.0″ encoding=”utf-8″?>
<android.support.constraint.ConstraintLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:app=”http://schemas.android.com/apk/res-auto”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.EventDetail”>

</android.support.constraint.ConstraintLayout>

Android系统布局—android.R.layout详解

布局文件,作为android中必不可少的一部分,android系统为了方便开发人员,在系统中定义了很多的布局文件。

系统布局文件和我们自定义的布局在写法用前缀android以示区别:

系统布局文件:android.R.layout.xxx;

用户自定义布局文件:R.layout.xxx;

那系统布局文件究竟有哪一些,大家在用的时候如果不了解,心里估计有点惴惴。现在下方图中列出所有系统布局,我们一一试用:

%title插图%num

下面我们会以代码来解释上面图片中涉及到的布局意义(按顺序描述):

先插入代码与主布局,如下:

  1. public class MainActivity extends Activity {
  2. ListView listView;
  3. List<String> listStrings;
  4. ArrayAdapter<String> arrayAdapter;
  5. SimpleAdapter simpleAdapter;
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_main);
  10. init();
  11. }
  12. public void init() {
  13. listView=(ListView)findViewById(R.id.mylistview);
  14. listStrings=new ArrayList<String>();
  15. listStrings.add(“千山鸟飞*”);
  16. listStrings.add(“万径人踪灭”);
  17. listStrings.add(“孤舟蓑笠翁”);
  18. listStrings.add(“独钓寒江雪”);
  19. arrayAdapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.activity_list_item, android.R.id.text1,listStrings);
  20. listView.setAdapter(arrayAdapter);
  21. arrayAdapter.notifyDataSetChanged();
  22. }
  23. }
activity_main布局:
  1. <RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
  2. xmlns:tools=“http://schemas.android.com/tools”
  3. android:layout_width=“match_parent”
  4. android:layout_height=“match_parent”
  5. android:paddingBottom=“@dimen/activity_vertical_margin”
  6. android:paddingLeft=“@dimen/activity_horizontal_margin”
  7. android:paddingRight=“@dimen/activity_horizontal_margin”
  8. android:paddingTop=“@dimen/activity_vertical_margin”
  9. tools:context=“.MainActivity” >
  10. <ListView
  11. android:id=“@+id/mylistview”
  12. android:layout_width=“wrap_content”
  13. android:layout_height=“match_parent”
  14. />
  15. </RelativeLayout>

 

很简单的一段代码,只在主界面显示一个listview,用于后续的测试。

1、activity_list_item

在代码中写法如下:

arrayAdapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.activity_list_item, android.R.id.text1,listStrings);

我们关注的是activity_list_item内容是:

 

  1. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
  2. android:layout_width=“match_parent”
  3. android:layout_height=“wrap_content”
  4. android:paddingTop=“1dip”
  5. android:paddingBottom=“1dip”
  6. android:paddingStart=“8dip”
  7. android:paddingEnd=“8dip”>
  8. <ImageView android:id=“@+id/icon”
  9. android:layout_width=“24dip”
  10. android:layout_height=“24dip”/>
  11. <TextView android:id=“@android:id/text1”
  12. android:layout_width=“wrap_content”
  13. android:layout_height=“wrap_content”
  14. android:layout_gravity=“center_horizontal”
  15. android:paddingStart=“?android:attr/listPreferredItemPaddingStart” />
  16. </LinearLayout>

 

可以看出这是一图一文字的一个布局,而我们写的时候并没有将图片加载进去。上面的写法可以达到一个效果,只显示文字。如果需要显示图片,我们还需后续手动编代码加入,所以是不是觉得麻烦。是的,个人不建议使用这个布局在arrayadapter中。这个布局并不比我们自定义的布局方便。

2、browser_link_context_header

同样的写法:

arrayAdapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.browser_link_context_header,listStrings);

我们看看android.R.layout.browser_link_context_header的内容:

  1. <TextView xmlns:android=“http://schemas.android.com/apk/res/android”
  2. android:id=“@+id/title”
  3. android:textAppearance=“?android:attr/textAppearanceLarge”
  4. android:textColor=“@color/white”
  5. android:layout_width=“wrap_content”
  6. android:layout_height=“wrap_content”
  7. android:maxLines=“2”
  8. android:paddingStart=“10dip”
  9. android:paddingEnd=“10dip”
  10. />

只是一个TextView,看布局中规定了字体颜色为白色,无其他效果。在底色为黑色或者暗色的情况下适用。

3、browser_link_context_header

  1. <TextView xmlns:android=“http://schemas.android.com/apk/res/android”
  2. android:id=“@+id/title”
  3. android:textAppearance=“?android:attr/textAppearanceLarge”
  4. android:textColor=“@color/white”
  5. android:layout_width=“wrap_content”
  6. android:layout_height=“wrap_content”
  7. android:maxLines=“2”
  8. android:paddingStart=“10dip”
  9. android:paddingEnd=“10dip”
  10. />

这个布局与上一个大同小异,不再多做说明。

4、expandable_list_content

  1. <ExpandableListView xmlns:android=“http://schemas.android.com/apk/res/android”
  2. android:id=“@android:id/list”
  3. android:layout_width=“match_parent”
  4. android:layout_height=“match_parent”
  5. android:drawSelectorOnTop=“false” />

 

这个布局与前几个都不相同,是一个可扩张的listview。但实际使用中没有特别的意义,并不如个人自定义写法方便。弃之。

如果要使用,可类似:inflate(MainActivity.this,  android.R.layout.expandable_list_content,null);

5、list_content

  1. <FrameLayout xmlns:android=“http://schemas.android.com/apk/res/android”
  2. android:layout_width=“match_parent”
  3. android:layout_height=“match_parent”>
  4. <LinearLayout android:id=“@+id/progressContainer”
  5. android:orientation=“vertical”
  6. android:layout_width=“match_parent”
  7. android:layout_height=“match_parent”
  8. android:visibility=“gone”
  9. android:gravity=“center”>
  10. <ProgressBar style=“?android:attr/progressBarStyleLarge”
  11. android:layout_width=“wrap_content”
  12. android:layout_height=“wrap_content” />
  13. <TextView android:layout_width=“wrap_content”
  14. android:layout_height=“wrap_content”
  15. android:textAppearance=“?android:attr/textAppearanceSmall”
  16. android:text=“@string/loading”
  17. android:paddingTop=“4dip”
  18. android:singleLine=“true” />
  19. </LinearLayout>
  20. <FrameLayout android:id=“@+id/listContainer”
  21. android:layout_width=“match_parent”
  22. android:layout_height=“match_parent”>
  23. <ListView android:id=“@android:id/list”
  24. android:layout_width=“match_parent”
  25. android:layout_height=“match_parent”
  26. android:drawSelectorOnTop=“false” />
  27. <TextView android:id=“@+android:id/internalEmpty”
  28. android:layout_width=“match_parent”
  29. android:layout_height=“match_parent”
  30. android:gravity=“center”
  31. android:textAppearance=“?android:attr/textAppearanceLarge” />
  32. </FrameLayout>
  33. </FrameLayout>

这个布局显的较为复杂,而实用性也很一般。如果需要达到这样的效果,使用这个布局,建议将这个布局拷贝做成自定义布局,方便你取数、赋值。

6、preference_category

  1. <TextView xmlns:android=“http://schemas.android.com/apk/res/android”
  2. style=“?android:attr/listSeparatorTextViewStyle”
  3. android:id=“@+android:id/title”
  4. />

这个布局,使用了一个style,写法:

 

arrayAdapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.preference_category,android.R.id.title,listStrings);

效果如下:

%title插图%num

分割线加粗、字体变化。在某些列表中可用。

7、select_dialog_item

  1. <TextView xmlns:android=“http://schemas.android.com/apk/res/android”
  2. android:id=“@android:id/text1”
  3. android:layout_width=“match_parent”
  4. android:layout_height=“wrap_content”
  5. android:minHeight=“?android:attr/listPreferredItemHeight”
  6. android:textAppearance=“?android:attr/textAppearanceLarge”
  7. android:textColor=“?android:attr/textColorAlertDialogListItem”
  8. android:gravity=“center_vertical”
  9. android:paddingStart=“14dip”
  10. android:paddingEnd=“15dip”
  11. android:ellipsize=“marquee”
  12. />

写法:

arrayAdapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.select_dialog_item,listStrings);

也是对字体、宽高等的一些设置,没有特殊变化。

8、select_dialog_multichoice

  1. <CheckedTextView xmlns:android=“http://schemas.android.com/apk/res/android”
  2. android:id=“@android:id/text1”
  3. android:layout_width=“match_parent”
  4. android:layout_height=“wrap_content”
  5. android:minHeight=“?android:attr/listPreferredItemHeight”
  6. android:textAppearance=“?android:attr/textAppearanceLarge”
  7. android:textColor=“?android:attr/textColorAlertDialogListItem”
  8. android:gravity=“center_vertical”
  9. android:paddingStart=“12dip”
  10. android:paddingEnd=“7dip”
  11. android:checkMark=“?android:attr/listChoiceIndicatorMultiple”
  12. android:ellipsize=“marquee”
  13. />

写法:

arrayAdapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.select_dialog_multichoice,listStrings);

 

这里出现了一个新的控件,CheckTextView。我们看看这个的效果和样式和以前的TextView是不同的,另外还请注意这个multichoice标识。

效果如下:

%title插图%num
这是一个可多选的效果。在项目中,这样的样式大家应该不陌生,这个布局对我们来说有一定意义!

9、select_dialog_singlechoice

从字面意思可以看到,这个与第8个的区别,在乎单选与多选:

  1. <CheckedTextView xmlns:android=“http://schemas.android.com/apk/res/android”
  2. android:id=“@android:id/text1”
  3. android:layout_width=“match_parent”
  4. android:layout_height=“wrap_content”
  5. android:minHeight=“?android:attr/listPreferredItemHeight”
  6. android:textAppearance=“?android:attr/textAppearanceLarge”
  7. android:textColor=“?android:attr/textColorAlertDialogListItem”
  8. android:gravity=“center_vertical”
  9. android:paddingStart=“12dip”
  10. android:paddingEnd=“7dip”
  11. android:checkMark=“?android:attr/listChoiceIndicatorSingle”
  12. android:ellipsize=“marquee”
  13. />

写法:

arrayAdapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.select_dialog_singlechoice,listStrings);

效果:

%title插图%num

10、simple_dropdown_item_1line

  1. <TextView xmlns:android=“http://schemas.android.com/apk/res/android”
  2. android:id=“@android:id/text1”
  3. style=“?android:attr/dropDownItemStyle”
  4. android:textAppearance=“?android:attr/textAppearanceLargePopupMenu”
  5. android:singleLine=“true”
  6. android:layout_width=“match_parent”
  7. android:layout_height=“?android:attr/listPreferredItemHeight”
  8. android:ellipsize=“marquee” />

 

写法:

arrayAdapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line,listStrings);

与上方其他的TextView类似,不再说明。

 

11、simple_expandable_list_item_1

  1. <TextView xmlns:android=“http://schemas.android.com/apk/res/android”
  2. android:id=“@android:id/text1”
  3. android:layout_width=“match_parent”
  4. android:layout_height=“?android:attr/listPreferredItemHeight”
  5. android:paddingStart=“?android:attr/expandableListPreferredItemPaddingLeft”
  6. android:textAppearance=“?android:attr/textAppearanceListItem”
  7. android:gravity=“center_vertical”
  8. android:textAlignment=“viewStart”
  9. />

 

写法:

arrayAdapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_expandable_list_item_1,listStrings);

不再说明。

12、simple_gallery_item

  1. <TextView xmlns:android=“http://schemas.android.com/apk/res/android” android:id=“@android:id/text1”
  2. android:textAppearance=“?android:attr/textAppearanceMedium”
  3. android:textColor=“?android:attr/textColorPrimaryDisableOnly”
  4. android:layout_width=“wrap_content”
  5. android:layout_height=“wrap_content”
  6. android:maxLines=“1” />

写法:

arrayAdapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_gallery_item,listStrings);

 

不再说明。

13、simple_list_item_1

  1. <TextView xmlns:android=“http://schemas.android.com/apk/res/android”
  2. android:id=“@android:id/text1”
  3. android:layout_width=“match_parent”
  4. android:layout_height=“wrap_content”
  5. android:textAppearance=“?android:attr/textAppearanceListItemSmall”
  6. android:gravity=“center_vertical”
  7. android:paddingStart=“?android:attr/listPreferredItemPaddingStart”
  8. android:paddingEnd=“?android:attr/listPreferredItemPaddingEnd”
  9. android:minHeight=“?android:attr/listPreferredItemHeightSmall”
  10. />

写法:

arrayAdapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,listStrings);

这里多说一句,这个应该是我们平常使用*多的一个系统布局文件,习惯成自然。

14、simple_list_item_activated_1

  1. <TextView xmlns:android=“http://schemas.android.com/apk/res/android”
  2. android:id=“@android:id/text1”
  3. android:layout_width=“match_parent”
  4. android:layout_height=“wrap_content”
  5. android:textAppearance=“?android:attr/textAppearanceListItemSmall”
  6. android:gravity=“center_vertical”
  7. android:paddingStart=“?android:attr/listPreferredItemPaddingStart”
  8. android:paddingEnd=“?android:attr/listPreferredItemPaddingEnd”
  9. android:background=“?android:attr/activatedBackgroundIndicator”
  10. android:minHeight=“?android:attr/listPreferredItemHeightSmall”
  11. />

写法:

arrayAdapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_activated_1,listStrings);

不再说明。

 

15、simple_list_item_multiple_choice

 

  1. <CheckedTextView xmlns:android=“http://schemas.android.com/apk/res/android”
  2. android:id=“@android:id/text1”
  3. android:layout_width=“match_parent”
  4. android:layout_height=“?android:attr/listPreferredItemHeightSmall”
  5. android:textAppearance=“?android:attr/textAppearanceListItemSmall”
  6. android:gravity=“center_vertical”
  7. android:checkMark=“?android:attr/listChoiceIndicatorMultiple”
  8. android:paddingStart=“?android:attr/listPreferredItemPaddingStart”
  9. android:paddingEnd=“?android:attr/listPreferredItemPaddingEnd”
  10. />

这个对我们来说也已经没有新鲜感了,因为在上面,我们有见过类似的了,多选的TextView。
写法:

arrayAdapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_multiple_choice,listStrings);

16、simple_list_item_single_choice

  1. <CheckedTextView xmlns:android=“http://schemas.android.com/apk/res/android”
  2. android:id=“@android:id/text1”
  3. android:layout_width=“match_parent”
  4. android:layout_height=“?android:attr/listPreferredItemHeightSmall”
  5. android:textAppearance=“?android:attr/textAppearanceListItemSmall”
  6. android:gravity=“center_vertical”
  7. android:checkMark=“?android:attr/listChoiceIndicatorSingle”
  8. android:paddingStart=“?android:attr/listPreferredItemPaddingStart”
  9. android:paddingEnd=“?android:attr/listPreferredItemPaddingEnd”
  10. />

写法:

arrayAdapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_single_choice,listStrings);

不再说明。

17、simple_list_item_checked
看这个带一个checked后缀,有点特殊。先看看xml:

  1. <CheckedTextView xmlns:android=“http://schemas.android.com/apk/res/android”
  2. android:id=“@android:id/text1”
  3. android:layout_width=“match_parent”
  4. android:layout_height=“?android:attr/listPreferredItemHeightSmall”
  5. android:textAppearance=“?android:attr/textAppearanceListItemSmall”
  6. android:gravity=“center_vertical”
  7. android:checkMark=“?android:attr/textCheckMark”
  8. android:paddingStart=“?android:attr/listPreferredItemPaddingStart”
  9. android:paddingEnd=“?android:attr/listPreferredItemPaddingEnd”
  10. />

写法:

arrayAdapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_checked,listStrings);

效果:

 

%title插图%num

还是有些特殊效果的,选中打钩。这个也可注意使用。

18、simple_selectable_list_item

  1. <CheckedTextView xmlns:android=“http://schemas.android.com/apk/res/android”
  2. android:id=“@android:id/text1”
  3. android:layout_width=“match_parent”
  4. android:layout_height=“?android:attr/listPreferredItemHeight”
  5. android:textAppearance=“?android:attr/textAppearanceListItem”
  6. android:gravity=“center_vertical”
  7. android:background=“?android:attr/listChoiceBackgroundIndicator”
  8. android:paddingStart=“8dip”
  9. android:paddingEnd=“8dip”
  10. />

写法:

arrayAdapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_selectable_list_item,listStrings);

不再说明。

19、simple_spinner_dropdown_item

  1. <CheckedTextView xmlns:android=“http://schemas.android.com/apk/res/android”
  2. android:id=“@android:id/text1”
  3. style=“?android:attr/spinnerDropDownItemStyle”
  4. android:singleLine=“true”
  5. android:layout_width=“match_parent”
  6. android:layout_height=“?android:attr/dropdownListPreferredItemHeight”
  7. android:ellipsize=“marquee”
  8. android:textAlignment=“inherit”/>

写法:arrayAdapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item,listStrings);
不再说明。

20、simple_spinner_item

  1. <TextView xmlns:android=“http://schemas.android.com/apk/res/android”
  2. android:id=“@android:id/text1”
  3. style=“?android:attr/spinnerItemStyle”
  4. android:singleLine=“true”
  5. android:layout_width=“match_parent”
  6. android:layout_height=“wrap_content”
  7. android:ellipsize=“marquee”
  8. android:textAlignment=“inherit”/>

写法:

arrayAdapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item,listStrings);

 

不再说明。

21、test_list_item

  1. <TextView xmlns:android=“http://schemas.android.com/apk/res/android”
  2. android:id=“@android:id/text1”
  3. android:textAppearance=“?android:attr/textAppearanceSmall”
  4. android:paddingTop=“2dip”
  5. android:paddingBottom=“3dip”
  6. android:layout_width=“match_parent”
  7. android:layout_height=“wrap_content”
  8. />

 

写法:

arrayAdapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.test_list_item,listStrings);

无特殊,不做说明。

 

到这里为止,我们单行的显示已经做到头了。。汗。同质的内容太多了。。

现在来一些不太一样的,被我们略过的几个布局。为了测试这几个布局的不同之处,我们修改下代码如下:

 


  1. public class MainActivity extends Activity {
  2. ListView listView;
  3. List<String> listStrings;
  4. ArrayAdapter<String> arrayAdapter;
  5. SimpleAdapter simpleAdapter;
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_main);
  10. init();
  11. }
  12. public void init() {
  13. listView=(ListView)findViewById(R.id.mylistview);
  14. List<Map<String, String>> listmaps=new ArrayList<Map<String,String>>();
  15. Map<String, String> map=new HashMap<String, String>();
  16. map.put(“first”, “*句”);
  17. map.put(“second”, “第二句”);
  18. listmaps.add(map);
  19. simpleAdapter=new SimpleAdapter(MainActivity.this, listmaps, android.R.layout.simple_expandable_list_item_2, new String[]{“first”,“second”}, new int[]{android.R.id.text1,android.R.id.text2});
  20. listView.setAdapter(simpleAdapter);
  21. simpleAdapter.notifyDataSetChanged();
  22. }
  23. }

代码不一样的地方在于,我们现在不适用ArrayAdapter来测试了,因为现在有两项内容了。ArrayAdapter已经不适合,我们用SimpleAdapter来测试。

22、simple_expandable_list_item_2

  1. <TwoLineListItem xmlns:android=“http://schemas.android.com/apk/res/android”
  2. android:layout_width=“match_parent”
  3. android:layout_height=“?android:attr/listPreferredItemHeight”
  4. android:paddingTop=“2dip”
  5. android:paddingBottom=“2dip”
  6. android:paddingStart=“?android:attr/expandableListPreferredItemPaddingLeft”
  7. android:mode=“twoLine”
  8. >
  9. <TextView
  10. android:id=“@android:id/text1”
  11. android:layout_width=“match_parent”
  12. android:layout_height=“wrap_content”
  13. android:layout_marginTop=“6dip”
  14. android:textAppearance=“?android:attr/textAppearanceListItem”
  15. android:textAlignment=“viewStart”
  16. />
  17. <TextView
  18. android:id=“@android:id/text2”
  19. android:layout_width=“match_parent”
  20. android:layout_height=“wrap_content”
  21. android:layout_below=“@android:id/text1”
  22. android:layout_alignStart=“@android:id/text1”
  23. android:textAppearance=“?android:attr/textAppearanceSmall”
  24. android:textAlignment=“viewStart”
  25. />
  26. </TwoLineListItem>

这是有两行TextView的一个布局,一上一下,一大一小。

写法:

simpleAdapter=new SimpleAdapter(MainActivity.this, listmaps, android.R.layout.simple_expandable_list_item_2, new String[]{“first”,”second”}, new int[]{android.R.id.text1,android.R.id.text2});

效果:

%title插图%num

这个布局因为这特殊效果,也是较为实用的。

23、simple_list_item_2

  1. <TwoLineListItem xmlns:android=“http://schemas.android.com/apk/res/android”
  2. android:layout_width=“match_parent”
  3. android:layout_height=“wrap_content”
  4. android:minHeight=“?android:attr/listPreferredItemHeight”
  5. android:mode=“twoLine”
  6. android:paddingStart=“?android:attr/listPreferredItemPaddingStart”
  7. android:paddingEnd=“?android:attr/listPreferredItemPaddingEnd”
  8. >
  9. <TextView android:id=“@android:id/text1”
  10. android:layout_width=“match_parent”
  11. android:layout_height=“wrap_content”
  12. android:layout_marginTop=“8dip”
  13. android:textAppearance=“?android:attr/textAppearanceListItem”
  14. />
  15. <TextView android:id=“@android:id/text2”
  16. android:layout_width=“match_parent”
  17. android:layout_height=“wrap_content”
  18. android:layout_below=“@android:id/text1”
  19. android:layout_alignStart=“@android:id/text1”
  20. android:textAppearance=“?android:attr/textAppearanceSmall”
  21. />
  22. </TwoLineListItem>

写法:

simpleAdapter=new SimpleAdapter(MainActivity.this, listmaps, android.R.layout.simple_list_item_2, new String[]{“first”,”second”}, new int[]{android.R.id.text1,android.R.id.text2});

效果与上一个类似,不再说明。

 

24、simple_list_item_activated_2

  1. <TwoLineListItem xmlns:android=“http://schemas.android.com/apk/res/android”
  2. android:paddingTop=“2dip”
  3. android:paddingBottom=“2dip”
  4. android:layout_width=“match_parent”
  5. android:layout_height=“wrap_content”
  6. android:background=“?android:attr/activatedBackgroundIndicator”
  7. android:minHeight=“?android:attr/listPreferredItemHeight”
  8. android:mode=“twoLine”
  9. >
  10. <TextView android:id=“@android:id/text1”
  11. android:layout_width=“match_parent”
  12. android:layout_height=“wrap_content”
  13. android:layout_marginStart=“?android:attr/listPreferredItemPaddingStart”
  14. android:layout_marginTop=“6dip”
  15. android:textAppearance=“?android:attr/textAppearanceListItem”
  16. />
  17. <TextView android:id=“@android:id/text2”
  18. android:layout_width=“match_parent”
  19. android:layout_height=“wrap_content”
  20. android:layout_below=“@android:id/text1”
  21. android:layout_alignStart=“@android:id/text1”
  22. android:textAppearance=“?android:attr/textAppearanceSmall”
  23. />
  24. </TwoLineListItem>

写法:

simpleAdapter=new SimpleAdapter(MainActivity.this, listmaps, android.R.layout.simple_list_item_activated_2, new String[]{“first”,”second”}, new int[]{android.R.id.text1,android.R.id.text2});

不再说明,效果与上类似。

 

25、two_line_list_item

  1. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
  2. android:layout_width=“match_parent”
  3. android:layout_height=“wrap_content”
  4. android:orientation=“vertical”>
  5. <TextView android:id=“@android:id/text1”
  6. android:textSize=“16sp”
  7. android:textStyle=“bold”
  8. android:layout_width=“match_parent”
  9. android:layout_height=“wrap_content”/>
  10. <TextView android:id=“@android:id/text2”
  11. android:textSize=“16sp”
  12. android:layout_width=“match_parent”
  13. android:layout_height=“wrap_content”/>
  14. </LinearLayout>

布局稍有不同啊,写法:

simpleAdapter=new SimpleAdapter(MainActivity.this, listmaps, android.R.layout.two_line_list_item, new String[]{“first”,”second”}, new int[]{android.R.id.text1,android.R.id.text2});

看一下效果:

%title插图%num

字体大小一致,上下颜色深浅不一。与前三个相比,还是有较大区别的。选用。

Android系统五大布局详解Layout

我们知道Android系统应用程序一般是由多个Activity组成,而这些Activity以视图的形式展现在我们面前,视图都是由一个一个的组件构成的。组件就是我们常见的Button、TextEdit等等。那么我们平时看到的Android手机中那些漂亮的界面是怎么显示出来的呢?这就要用到Android的布局管理器了,网上有人比喻的很好:布局好比是建筑里的框架,组件按照布局的要求依次排列,就组成了用于看见的漂亮界面了。

在分析布局之前,我们首先看看控件:Android中任何可视化的控件都是从android.veiw.View继承而来的,系统提供了两种方法来设置视图:*种也是我们*常用的的使用XML文件来配置View的相关属性,然后在程序启动时系统根据配置文件来创建相应的View视图。第二种是我们在代码中直接使用相应的类来创建视图。

如何使用XML文件定义视图:

每个Android项目的源码目录下都有个res/layout目录,这个目录就是用来存放布局文件的。布局文件一般以对应activity的名字命名,以 .xml 为后缀。在xml中为创建组件时,需要为组件指定id,如:android:id=”@+id/名字”系统会自动在gen目录下创建相应的R资源类变量。

如何在代码中使用视图:

在代码中创建每个Activity时,一般是在onCreate()方法中,调用setContentView()来加载指定的xml布局文件,然后就可以通过findViewById()来获得在布局文件中创建的相应id的控件了,如Button等。

如:

[html]  view plain copy

  1. private Button btnSndMag;
  2. public void onCreate(Bundle savedInstanceState) {
  3.     super.onCreate(savedInstanceState);
  4.     setContentView(R.layout.main);  // 加载main.xml布局文件
  5.     btnSndMag = (Button)this.findViewById(R.id.btnSndMag); // 通过id找到对于的Button组件
  6.     ….
  7. }

 

下面我们来介绍Android系统中为我们提供的五大布局:LinearLayout(线性布局)、FrameLayout(单帧布局)、AbsoluteLayout(*对布局)、TablelLayout(表格布局)、RelativeLayout(相对布局)。其中*常用的的是LinearLayout、TablelLayout和RelativeLayout。这些布局都可以嵌套使用。

(1)LinearLayout 线性布局

线性布局是按照水平或垂直的顺序将子元素(可以是控件或布局)依次按照顺序排列,每一个元素都位于前面一个元素之后。线性布局分为两种:水平方向和垂直方向的布局。分别通过属性android:orientation=”vertical” 和 android:orientation=”horizontal”来设置。

android:layout_weight 表示子元素占据的空间大小的比例,有人说这个值大小和占据空间成正比,有人说反比。我在实际应用中设置和网上资料显示的刚好相反,这个问题后面会专门写一篇文章来分析。现在我们只需要按照正比例来设置就可以。

例如下面我们实现一个如图所示的简易计算器界面:

%title插图%num

代码:

[html]  view plain copy

  1. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
  2.     xmlns:tools=“http://schemas.android.com/tools”
  3.     android:orientation=“vertical”
  4.     android:layout_width=“match_parent”
  5.     android:layout_height=“match_parent”
  6.     android:background=“#FFFFFF”
  7.     tools:context=“.MainActivity” >
  8.     // 这里*行显示标签为一个水平布局
  9.     <LinearLayout
  10.         android:layout_width=“match_parent”
  11.         android:layout_height=“wrap_content”
  12.         android:orientation=“horizontal” >
  13.         <EditText
  14.             android:id=“@+id/msg”
  15.             android:inputType=“number”
  16.             android:layout_width=“match_parent”
  17.             android:layout_height=“wrap_content”
  18.             android:text=“”>
  19.         </EditText>
  20.     </LinearLayout>
  21.     // 第二行为 mc m+ m- mr 四个Button构成一个水平布局
  22.     <LinearLayout
  23.         android:layout_width=“match_parent”
  24.         android:layout_height=“wrap_content”
  25.         android:orientation=“horizontal” >
  26.         <Button
  27.             android:layout_width=“match_parent”
  28.             android:layout_height=“wrap_content”
  29.             android:text=“mc” android:layout_weight=“1”>
  30.         </Button>
  31.         <Button
  32.             android:layout_width=“match_parent”
  33.             android:layout_height=“wrap_content”
  34.             android:text=“m+” android:layout_weight=“1”>
  35.         </Button>
  36.         <Button
  37.             android:layout_width=“match_parent”
  38.             android:layout_height=“wrap_content”
  39.             android:text=“m-“ android:layout_weight=“1”>
  40.         </Button>
  41.         <Button
  42.             android:layout_width=“match_parent”
  43.             android:layout_height=“wrap_content”
  44.             android:text=“mr” android:layout_weight=“1”>
  45.         </Button>
  46.     </LinearLayout>
  47.     // 同上 C +/-  / * 四个Button构成一个水平布局
  48.       <LinearLayout
  49.           android:layout_width=“match_parent”
  50.           android:layout_height=“wrap_content”
  51.           android:orientation=“horizontal” >
  52.           <Button
  53.               android:layout_width=“match_parent”
  54.               android:layout_height=“wrap_content”
  55.               android:layout_weight=“1”
  56.               android:text=“C” >
  57.           </Button>
  58.           <Button
  59.               android:layout_width=“match_parent”
  60.               android:layout_height=“wrap_content”
  61.               android:layout_weight=“1”
  62.               android:text=“+/-“ >
  63.           </Button>
  64.           <Button
  65.               android:layout_width=“match_parent”
  66.               android:layout_height=“wrap_content”
  67.               android:layout_weight=“1”
  68.               android:text=“/” >
  69.           </Button>
  70.           <Button
  71.               android:layout_width=“match_parent”
  72.               android:layout_height=“wrap_content”
  73.               android:layout_weight=“1”
  74.               android:text=“*” >
  75.           </Button>
  76.       </LinearLayout>
  77.       <LinearLayout
  78.         android:layout_width=“match_parent”
  79.         android:layout_height=“wrap_content”
  80.         android:orientation=“horizontal” >
  81.         <Button
  82.             android:layout_width=“match_parent”
  83.             android:layout_height=“wrap_content”
  84.             android:text=“7” android:layout_weight=“1”>
  85.         </Button>
  86.         <Button
  87.             android:layout_width=“match_parent”
  88.             android:layout_height=“wrap_content”
  89.             android:text=“8” android:layout_weight=“1”>
  90.         </Button>
  91.         <Button
  92.             android:layout_width=“match_parent”
  93.             android:layout_height=“wrap_content”
  94.             android:text=“9” android:layout_weight=“1”>
  95.         </Button>
  96.         <Button
  97.             android:layout_width=“match_parent”
  98.             android:layout_height=“wrap_content”
  99.             android:text=“-“ android:layout_weight=“1”>
  100.         </Button>
  101.     </LinearLayout>
  102.     <LinearLayout
  103.         android:layout_width=“match_parent”
  104.         android:layout_height=“wrap_content”
  105.         android:orientation=“horizontal” >
  106.         <Button
  107.             android:layout_width=“match_parent”
  108.             android:layout_height=“wrap_content”
  109.             android:layout_weight=“1”
  110.             android:text=“4” >
  111.         </Button>
  112.         <Button
  113.             android:layout_width=“match_parent”
  114.             android:layout_height=“wrap_content”
  115.             android:layout_weight=“1”
  116.             android:text=“5” >
  117.         </Button>
  118.         <Button
  119.             android:layout_width=“match_parent”
  120.             android:layout_height=“wrap_content”
  121.             android:layout_weight=“1”
  122.             android:text=“6” >
  123.         </Button>
  124.         <Button
  125.             android:layout_width=“match_parent”
  126.             android:layout_height=“wrap_content”
  127.             android:layout_weight=“1”
  128.             android:text=“+” >
  129.         </Button>
  130.     </LinearLayout>
  131.     // *外层是一个水平布局,由左边上面一行1 2 3三个Button,下面一行的0 . 两个Button 和 右边的=构成
  132.      <LinearLayout android:orientation=“horizontal”
  133.         android:layout_width=“match_parent”
  134.         android:layout_height=“wrap_content”>
  135.         // 这里 1 2 3 和 下面的 0 . 构成一个垂直布局
  136.         <LinearLayout android:orientation=“vertical”
  137.             android:layout_weight=“3”
  138.             android:layout_width=“wrap_content”
  139.             android:layout_height=“wrap_content”>
  140.             // 这里的 1 2 3 构成一个水平布局
  141.             <LinearLayout android:orientation=“horizontal”
  142.                 android:layout_width=“match_parent”
  143.                 android:layout_height=“wrap_content”>
  144.                 <Button
  145.                     android:layout_width=“wrap_content”
  146.                     android:layout_height=“wrap_content”
  147.                     android:layout_weight=“1”
  148.                     android:text=“1”></Button>
  149.                 <Button
  150.                     android:layout_width=“wrap_content”
  151.                     android:layout_height=“wrap_content”
  152.                     android:layout_weight=“1”
  153.                     android:text=“2”></Button>
  154.                 <Button
  155.                     android:layout_width=“wrap_content”
  156.                     android:layout_height=“wrap_content”
  157.                     android:layout_weight=“1”
  158.                     android:text=“3”></Button>
  159.             </LinearLayout>
  160.             // 这里的 0 和 . 构成一个水平布局,注意这里的android_weight参数设置
  161.             <LinearLayout android:orientation=“horizontal”
  162.                 android:layout_width=“match_parent”
  163.                 android:layout_height=“wrap_content”>
  164.                 <Button
  165.                     android:layout_width=“0px”
  166.                     android:layout_height=“wrap_content”
  167.                     android:layout_weight=“2”
  168.                     android:text=“0”></Button>
  169.                 <Button
  170.                     android:layout_width=“0px”
  171.                     android:layout_height=“wrap_content”
  172.                     android:layout_weight=“1”
  173.                     android:text=“.”></Button>
  174.             </LinearLayout>
  175.         </LinearLayout>
  176.         // 这里一个单独Button构成的垂直布局
  177.         <LinearLayout android:orientation=“vertical”
  178.             android:layout_weight=“1”
  179.             android:layout_width=“wrap_content”
  180.             android:layout_height=“match_parent”>
  181.             <Button
  182.                 android:layout_width=“match_parent”
  183.                 android:layout_height=“match_parent”
  184.                 android:text=“=”></Button>
  185.         </LinearLayout>
  186.      </LinearLayout>
  187. </LinearLayout>

(2)TableLayout 表格布局

表格布局,适用于多行多列的布局格式,每个TableLayout是由多个TableRow组成,一个TableRow就表示TableLayout中的每一行,这一行可以由多个子元素组成。实际上TableLayout和TableRow都是LineLayout线性布局的子类。但是TableRow的参数android:orientation属性值固定为horizontal,且android:layout_width=MATCH_PARENT,android:layout_height=WRAP_CONTENT。所以TableRow实际是一个横向的线性布局,且所以子元素宽度和高度一致。

注意:在TableLayout中,单元格可以为空,但是不能跨列,意思是只能不能有相邻的单元格为空。

在TableLayout布局中,一列的宽度由该列中*宽的那个单元格指定,而该表格的宽度由父容器指定。可以为每一列设置以下属性:

Shrinkable  表示该列的宽度可以进行收缩,以使表格能够适应父容器的大小

Stretchable 表示该列的宽度可以进行拉伸,以使能够填满表格中的空闲空间

Collapsed  表示该列会被隐藏

TableLayout中的特有属性:

android:collapseColumns

android:shrinkColumns

android:stretchColumns = “0,1,2,3”// 表示产生4个可拉伸的列

Demo:我们想设计一个如下所以的一个三行三列的表格,但是第二行我们只想显示2个表格:

%title插图%num

[java]  view plain copy

  1. <?xml version=“1.0” encoding=“utf-8”?>
  2. <TableLayout xmlns:android=“http://schemas.android.com/apk/res/android”
  3.     android:orientation=“vertical”
  4.     android:shrinkColumns=“0,1,2”  // 设置三列都可以收缩  
  5.     android:stretchColumns=“0,1,2” // 设置三列都可以拉伸 如果不设置这个,那个显示的表格将不能填慢整个屏幕
  6.     android:layout_width=“fill_parent”
  7.     android:layout_height=“fill_parent” >
  8.     <TableRow android:layout_width=“fill_parent”
  9.         android:layout_height=“wrap_content”>
  10.         <Button android:gravity=“center”
  11.             android:padding=“10dp”
  12.             android:text=“Button1”>
  13.         </Button>
  14.         <Button android:gravity=“center”
  15.             android:padding=“10dp”
  16.             android:text=“Button2”>
  17.         </Button>
  18.         <Button android:gravity=“center”
  19.             android:padding=“10dp”
  20.             android:text=“Button3”>
  21.         </Button>
  22.     </TableRow>
  23.     <TableRow android:layout_width=“fill_parent”
  24.         android:layout_height=“wrap_content”>
  25.         <Button android:gravity=“center”
  26.             android:padding=“10dp”
  27.             android:text=“Button4”>
  28.         </Button>
  29.         <Button android:gravity=“center”
  30.             android:padding=“10dp”
  31.             android:text=“Button5”>
  32.         </Button>
  33.     </TableRow>
  34.     <TableRow android:layout_width=“fill_parent”
  35.         android:layout_height=“wrap_content”>
  36.         <Button android:gravity=“center”
  37.             android:padding=“10dp”
  38.             android:text=“Button6”>
  39.         </Button>
  40.         <Button android:gravity=“center”
  41.             android:padding=“10dp”
  42.             android:text=“Button7”>
  43.         </Button>
  44.         <Button android:gravity=“center”
  45.             android:padding=“10dp”
  46.             android:text=“Button8”>
  47.         </Button>
  48.     </TableRow>
  49. </TableLayout>

 

(3)RelativeLayout 相对布局

RelativeLayout继承于android.widget.ViewGroup,其按照子元素之间的位置关系完成布局的,作为Android系统五大布局中*灵活也是*常用的一种布局方式,非常适合于一些比较复杂的界面设计。

注意:在引用其他子元素之前,引用的ID必须已经存在,否则将出现异常。

常用的位置属性:

 

[java]  view plain copy

  1. android:layout_toLeftOf         该组件位于引用组件的左方
  2. android:layout_toRightOf        该组件位于引用组件的右方
  3. android:layout_above            该组件位于引用组件的上方
  4. android:layout_below                该组件位于引用组件的下方
  5. android:layout_alignParentLeft      该组件是否对齐父组件的左端
  6. android:layout_alignParentRight     该组件是否齐其父组件的右端
  7. android:layout_alignParentTop       该组件是否对齐父组件的顶部
  8. android:layout_alignParentBottom    该组件是否对齐父组件的底部
  9. android:layout_centerInParent       该组件是否相对于父组件居中
  10. android:layout_centerHorizontal     该组件是否横向居中
  11. android:layout_centerVertical       该组件是否垂直居中

 

Demo:利用相对布局设计一个如下图所示的界面:

%title插图%num

源码:

[html]  view plain copy

  1. <?xml version=“1.0” encoding=“utf-8”?>
  2. <RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
  3.     android:layout_width=“fill_parent”
  4.     android:layout_height=“fill_parent” >
  5.     <Button android:id=“@+id/btn1”
  6.         android:layout_width=“wrap_content”
  7.         android:layout_height=“wrap_content”
  8.         android:layout_centerInParent=“true”
  9.         android:layout_centerHorizontal=“true”
  10.         android:text=“Button1”
  11.         ></Button>
  12.     <Button android:id=“@+id/btn2”
  13.         android:layout_width=“wrap_content”
  14.         android:layout_height=“wrap_content”
  15.         android:layout_toLeftOf=“@id/btn1”
  16.         android:layout_above=“@id/btn1”
  17.         android:text=“Button2”
  18.         ></Button>
  19.     <Button android:id=“@+id/btn3”
  20.         android:layout_width=“wrap_content”
  21.         android:layout_height=“wrap_content”
  22.         android:layout_toRightOf=“@id/btn1”
  23.         android:layout_above=“@id/btn1”
  24.         android:text=“Button3”
  25.         ></Button>
  26.     <Button android:id=“@+id/btn4”
  27.         android:layout_width=“wrap_content”
  28.         android:layout_height=“wrap_content”
  29.         android:layout_toRightOf=“@id/btn2”
  30.         android:layout_toLeftOf=“@id/btn3”
  31.         android:layout_above=“@id/btn2”
  32.         android:text=“Button4”
  33.         ></Button>
  34. </RelativeLayout>

(4)FrameLayout 框架布局

将所有的子元素放在整个界面的左上角,后面的子元素直接覆盖前面的子元素,所以用的比较少。

(5) AbsoluteLayou *对布局

*对布局中将所有的子元素通过设置android:layout_x 和 android:layout_y属性,将子元素的坐标位置固定下来,即坐标(android:layout_x, android:layout_y) ,layout_x用来表示横坐标,layout_y用来表示纵坐标。屏幕左上角为坐标(0,0),横向往右为正方,纵向往下为正方。实际应用中,这种布局用的比较少,因为Android终端一般机型比较多,各自的屏幕大小。分辨率等可能都不一样,如果用*对布局,可能导致在有的终端上显示不全等。

除上面讲过之外常用的几个布局的属性:
(1)layout_margin 
用于设置控件边缘相对于父控件的边距
android:layout_marginLeft
android:layout_marginRight
android:layout_marginTop
android:layout_marginBottom

(2) layout_padding 
用于设置控件内容相对于控件边缘的边距
android:layout_paddingLeft
android:layout_paddingRight
android:layout_paddingTop
android:layout_paddingBottom

(3) layout_width/height
用于设置控件的高度和宽度
wrap_content 内容包裹,表示这个控件的里面文字大小填充
fill_parent 跟随父窗口
match_parent

(4) gravity 
用于设置View组件里面内容的对齐方式
top bottom   left   right   center等

(5) android:layout_gravity  
用于设置Container组件的对齐方式
android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐
android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐
android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐
android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐