HappyIdiom之二主界面的设计

目标:实现HappyIdiom的主界面的设计


整体布局actvity_main.xml

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:padding="5dp" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp" >

            <TextView
                android:id="@+id/content1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="@dimen/padding_medium"
                android:text="@string/title_study" />

            <TextView
                android:id="@+id/content2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="@dimen/padding_medium"
                android:text="@string/title_search" />

            <TextView
                android:id="@+id/content3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="@dimen/padding_medium"
                android:text="@string/title_game" />

            <TextView
                android:id="@+id/content4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="@dimen/padding_medium"
                android:text="@string/title_save" />

            <TextView
                android:id="@+id/content5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="@dimen/padding_medium"
                android:text="@string/title_help" />
        </FrameLayout>
    </LinearLayout>

</TabHost>


MainActivity核心代码

/**
 * 主界面设计
 * @author cabbage
 *
 */
public class MainActivity extends TabActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//获取选项卡组
		TabHost tabHost = getTabHost();
		//创建每个选项卡
		TabHost.TabSpec tab1 = tabHost.newTabSpec("tab1").setIndicator(
				getString(R.string.title_study),
				getResources().getDrawable(R.drawable.study_study)).setContent(R.id.content1);
		TabHost.TabSpec tab2 = tabHost.newTabSpec("tab2").setIndicator(
				getString(R.string.title_search),
				getResources().getDrawable(R.drawable.study_search)).setContent(R.id.content2);
		TabHost.TabSpec tab3 = tabHost.newTabSpec("tab3").setIndicator(
				getString(R.string.title_game),
				getResources().getDrawable(R.drawable.study_game)).setContent(R.id.content3);
		TabHost.TabSpec tab4 = tabHost.newTabSpec("tab4").setIndicator(
				getString(R.string.title_save),
				getResources().getDrawable(R.drawable.study_save)).setContent(R.id.content4);
		TabHost.TabSpec tab5 = tabHost.newTabSpec("tab5").setIndicator(
				getString(R.string.title_help),
				getResources().getDrawable(R.drawable.study_help)).setContent(R.id.content5);
        tabHost.addTab(tab1);
        tabHost.addTab(tab2);
        tabHost.addTab(tab3);
        tabHost.addTab(tab4);
        tabHost.addTab(tab5);     
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
}

如何实现可滚动的标题栏呢?

  • 自定义标题栏布局 title_marquee.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">

	<TextView
		android:id="@+id/tv_showSave" android:layout_gravity="left|bottom"
		android:layout_width="fill_parent" android:layout_height="wrap_content"
		android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever"
		android:focusableInTouchMode="true" android:singleLine="true"
		android:focusable="true" android:textColor="#F5FFFA"
		android:scrollHorizontally="true"
		android:background="#7e7e7e" />

</LinearLayout>
  • 修改MainActivity
public class MainActivity extends TabActivity {
    private TextView showSave;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//注意此句要放在setContentView之前
		getWindow().requestFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.activity_main);
		//获取选项卡组
		TabHost tabHost = getTabHost();
		//创建每个选项卡
		TabHost.TabSpec tab1 = tabHost.newTabSpec("tab1").setIndicator(
				getString(R.string.title_study),
				getResources().getDrawable(R.drawable.study_study)).setContent(R.id.content1);
		TabHost.TabSpec tab2 = tabHost.newTabSpec("tab2").setIndicator(
				getString(R.string.title_search),
				getResources().getDrawable(R.drawable.study_search)).setContent(R.id.content2);
		TabHost.TabSpec tab3 = tabHost.newTabSpec("tab3").setIndicator(
				getString(R.string.title_game),
				getResources().getDrawable(R.drawable.study_game)).setContent(R.id.content3);
		TabHost.TabSpec tab4 = tabHost.newTabSpec("tab4").setIndicator(
				getString(R.string.title_save),
				getResources().getDrawable(R.drawable.study_save)).setContent(R.id.content4);
		TabHost.TabSpec tab5 = tabHost.newTabSpec("tab5").setIndicator(
				getString(R.string.title_help),
				getResources().getDrawable(R.drawable.study_help)).setContent(R.id.content5);
        tabHost.addTab(tab1);
        tabHost.addTab(tab2);
        tabHost.addTab(tab3);
        tabHost.addTab(tab4);
        tabHost.addTab(tab5);     
        //引入自定义标题的布局文件
         getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_marquee);
        showSave=(TextView) this.findViewById(R.id.tv_showSave);
        showSave.append("一心一意"+" ");
        showSave.append("三心二意"+" ");
        showSave.append("有情有意"+" ");
        showSave.append("一叶知秋"+" ");
        showSave.append("风和日丽"+" ");
        showSave.append("四平八稳"+" ");
        showSave.append("一叶障目"+" ");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值