具有灵活宽度的Android单选按钮
我目前正在尝试在android(3.1)上实现一个底部有标签的用户界面。我知道有几种方法,但是,因为我找不到适合我的方法,所以我尝试使用单选按钮。其思想是,每个选项卡都是一个单选按钮(当前没有文本) 布局应如下所示:具有灵活宽度的Android单选按钮,android,layout,user-interface,tabs,Android,Layout,User Interface,Tabs,我目前正在尝试在android(3.1)上实现一个底部有标签的用户界面。我知道有几种方法,但是,因为我找不到适合我的方法,所以我尝试使用单选按钮。其思想是,每个选项卡都是一个单选按钮(当前没有文本) 布局应如下所示: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- container for the fragments -->
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:layout_weight="0" />
<!-- container for the tabs realized with radio buttons -->
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:layout_weight="1" >
<!-- tabs -->
<RadioButton android:id="@+id/tab_1"
android:state_checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/tab_button"
android:text="" />
<RadioButton android:id="@+id/tab_2"
android:state_checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/tab_button"
android:text="" />
<RadioButton android:id="@+id/tab_3"
android:state_checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/tab_button"
android:text="" />
<RadioButton android:id="@+id/tab_4"
android:state_checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/tab_button"
android:text="" />
<RadioButton android:id="@+id/tab_5"
android:state_checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/tab_button"
android:text="" />
</RadioGroup>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize="false">
<item
android:state_checked="false"
android:drawable="@drawable/tab_inactive" />
<item
android:state_checked="true"
android:drawable="@drawable/tab_active" />
</selector>
屏幕顶部有一个framelayout,充当碎片的容器
在该容器下面有一个水平单选组,每个单选按钮都充当选项卡。活动选项卡(单选按钮)应与上部的其他选项卡重叠,以便清楚哪个选项卡处于活动状态。为了使单选按钮像选项卡一样工作,我将android:button属性设置为statelistdrawable。statelistdrawable包含两个活动和非活动状态的图像(不幸的是,我还不允许发布图像)。
由于活动选项卡的图像比非活动选项卡的图像大(宽度),我的想法是在onClickListener中使用bringToFront()方法,以便活动选项卡(或单选按钮)在其左右两侧重叠选项卡
我的布局文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- container for the fragments -->
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:layout_weight="0" />
<!-- container for the tabs realized with radio buttons -->
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:layout_weight="1" >
<!-- tabs -->
<RadioButton android:id="@+id/tab_1"
android:state_checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/tab_button"
android:text="" />
<RadioButton android:id="@+id/tab_2"
android:state_checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/tab_button"
android:text="" />
<RadioButton android:id="@+id/tab_3"
android:state_checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/tab_button"
android:text="" />
<RadioButton android:id="@+id/tab_4"
android:state_checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/tab_button"
android:text="" />
<RadioButton android:id="@+id/tab_5"
android:state_checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/tab_button"
android:text="" />
</RadioGroup>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize="false">
<item
android:state_checked="false"
android:drawable="@drawable/tab_inactive" />
<item
android:state_checked="true"
android:drawable="@drawable/tab_active" />
</selector>
statelistdrawable xml文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- container for the fragments -->
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:layout_weight="0" />
<!-- container for the tabs realized with radio buttons -->
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:layout_weight="1" >
<!-- tabs -->
<RadioButton android:id="@+id/tab_1"
android:state_checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/tab_button"
android:text="" />
<RadioButton android:id="@+id/tab_2"
android:state_checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/tab_button"
android:text="" />
<RadioButton android:id="@+id/tab_3"
android:state_checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/tab_button"
android:text="" />
<RadioButton android:id="@+id/tab_4"
android:state_checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/tab_button"
android:text="" />
<RadioButton android:id="@+id/tab_5"
android:state_checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/tab_button"
android:text="" />
</RadioGroup>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize="false">
<item
android:state_checked="false"
android:drawable="@drawable/tab_inactive" />
<item
android:state_checked="true"
android:drawable="@drawable/tab_active" />
</selector>
不幸的是,我遇到了两个问题:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!-- container for the fragments -->
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" />
<!-- container for the tabs realized with radio buttons -->
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:orientation="horizontal" >
<!-- tabs -->
<RadioButton android:id="@+id/tab_1"
android:state_checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@drawable/tab_button"
android:text="" />
<RadioButton android:id="@+id/tab_2"
android:state_checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@drawable/tab_button"
android:text="" />
</RadioGroup>
</FrameLayout>
我现在找到了第二个问题的解决方案(不是很优雅)。我在选项卡(单选按钮)和片段之间插入了一个分隔条,该分隔条根据哪个选项卡处于活动状态,为不同的状态使用levelListDrawable。留给我第一个问题第一个问题的答案见: