Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有灵活宽度的Android单选按钮_Android_Layout_User Interface_Tabs - Fatal编程技术网

具有灵活宽度的Android单选按钮

具有灵活宽度的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

我目前正在尝试在android(3.1)上实现一个底部有标签的用户界面。我知道有几种方法,但是,因为我找不到适合我的方法,所以我尝试使用单选按钮。其思想是,每个选项卡都是一个单选按钮(当前没有文本)

布局应如下所示:

<?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>

不幸的是,我遇到了两个问题:

  • 即使我在围绕这两个布局的linearlayout中将framelayout的布局权重设置为0,将radiogroup的布局权重设置为1,framelayout仍占据整个屏幕
  • 当我将framelayout的高度设置为0进行调试时,会显示选项卡。但是,它们并不是均匀分布在宽度上,而是相互重叠,并向左侧挤压。 当我将单选按钮的宽度设置为非活动选项卡所用图像的宽度(px)时,选项卡会均匀对齐。但是,当我单击其中一个选项卡时,选项卡图像会更改为活动选项卡的图像,该按钮不会与右侧或左侧的按钮重叠,即使我将statelistdrawable中的android:constantSize属性设置为false,但会将其向右移动
  • 有没有人能解决这些问题,特别是使用相对维度?这是我在这里的第一篇帖子,如果我的问题是要详细说明或者不够详细,我提前道歉。相信我,我已经尝试了很久来解决这些问题(例如使用绝对维度)。任何帮助都将不胜感激

    1.)使用FrameLayout作为包装布局。它会将放射组放置在子框架布局的顶部。从子FrameLayout和RadioGroup中删除布局权重属性

    2.)将射线组居中,并在其上使用wrap_内容,以及按钮(带一些填充的evt)(如果您的可绘制文件具有定义的设备相关宽度)。或者在所有按钮上使用布局_权重1

    <?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。留给我第一个问题第一个问题的答案见: