最简单的自定义按钮
imdb有类似这样的效果:
不是tab做的,是自定义按钮。
为了模仿它,先写了个最简单的自定义按钮的实现:
横屏效果:
这里先说说按钮如何做到横屏竖屏自动平分空间,使用的是布局的android:layout_weight属性。三个按钮都设置android:layout_weight=1,表示他们在水平布局的权重是均等的,这样就会平分空间。
布局的代码如下:
<?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">
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="100px"
android:focusable="true" android:focusableInTouchMode="true"
android:paddingBottom="2px">
<com.easymorse.videodemo.MyTextButton android:layout_width="wrap_content"
android:layout_height="match_parent" android:text="电影"
android:layout_weight="1" />
<Button android:layout_width="wrap_content"
android:layout_height="match_parent" android:text="电视"
android:layout_weight="1" />
<Button android:layout_width="wrap_content"
android:layout_height="match_parent" android:text="明星"
android:layout_weight="1" />
</LinearLayout>
<ListView android:id="@+id/list" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
其中,第一个按钮“电影”是自定义按钮,其实也没做啥自定义的操作,仅仅是写了个按钮类继承了Button类而已。需要注意的是构造方法,必须实现一个下面参数的构造方法:
package com.easymorse.videodemo;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.Button;public class MyTextButton extends Button {
public MyTextButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
源代码见:
这篇文章上的评论的 RSS feed TrackBack URI