如何将文本居中放入单选按钮

时间:2011-08-04 11:41:29

标签: android radio-button

我已经定制了具有特定背景的原始单选按钮,并删除了按钮:

styles.xml:

<style name="echelle_relecture">
    <item name="android:background">@drawable/btn_relecture_echelle</item>
    <item name="android:button">@null</item>
    <item name="android:layout_gravity">center_horizontal</item>
</style>

radio.xml:

<RadioGroup android:layout_height="wrap_content" android:id="@+id/radioGroup1" android:layout_width="fill_parent" android:orientation="horizontal" android:layout_marginTop="10dp">
        <RadioButton style="@style/echelle_relecture" android:text="@string/day" android:id="@+id/jour" android:checked="true"></RadioButton>
        <RadioButton style="@style/echelle_relecture" android:text="@string/hour" android:id="@+id/hour" android:button="@null"></RadioButton>
        <RadioButton style="@style/echelle_relecture" android:text="@string/min" android:id="@+id/mins" android:button="@null" android:enabled="false"></RadioButton>
</RadioGroup>

尽管有“center_horizo​​ntal”参数,但我无法将文本置于按钮内。我怎么能这样做?

5 个答案:

答案 0 :(得分:16)

您应该使用gravity而不是layout_gravity来集中RadioButton内的文字。

答案 1 :(得分:7)

使用此

android:gravity="center"

答案 2 :(得分:1)

def adds_omits(request, project_id):
    project = Project.objects.select_related('variations').prefetch_related('variations__addomit_set','variations__addomit_set__item').get(id=project_id)
    budget = get_current_budget(project_id)
    try: v = project.variations
    except ObjectDoesNotExist: v = Variations.objects.create(project=project)
    adds_omits = project.variations.addomit_set.select_related('item', 'item__project_room', 'item__project_room__room').all().order_by('item__build_type', 'item__build_type_detail', 'item__project_room', 'item__order', 'item__id') #Needs id to prevent other ordering within groups
    budget_items = BudgetItem.objects.select_related('addomit', 'addomit__variations', 'addomit__variations__project', 'project_room', 'project_room__room').filter(addomit__variations=v).order_by('build_type', 'build_type_detail', 'project_room', 'order', 'id') #Needs id to prevent other ordering within groups
    item_formset = BudgetItemFormsetLite(queryset=budget_items, form_kwargs={"ao":True,'project':project})
    ao_formset = AddOmitFormset(instance=v, queryset=adds_omits)
    formsets = zip(list(item_formset.forms), list(ao_formset.forms))

    for i_form in item_formset:
        for field in i_form.fields:
            if not field == 'id':
                i_form.fields[field].widget.attrs['readonly'] = True
                i_form.fields[field].widget.attrs['disabled'] = True

    #Used to create new add omits
    print 'Item form ini'
    item_form = BudgetItemForm(project=project, template=1)
    var_type_form = AoVariationTypeForm()

    context = {
        'project': project,
        'formsets': formsets,
        'ao_formset': ao_formset,
        'item_formset': item_formset,
        'var_type_form': var_type_form,
        'item_form': item_form,
        'widths': budget_item_column_widths[4:], #First column is add/delete options to allow for forloop count offset
        'options_width': "5em",  #First column is add/delete options to allow for forloop count offset
        'widths_ao': add_omit_column_widths,
        'widths_item': budget_item_column_widths, #First column is add/delete options to allow for forloop count offset
        'labour_rate': labour_day_rate,
        'skill_rate': skill_day_rate,

    }

    return render(request, 'costing/adds_omits.html', context)

答案 3 :(得分:0)

设置android:paddingLeft="0dp"android:paddingRight="0dp"应该可以使文本居中

答案 4 :(得分:0)

以您的样式添加代码 <item name="android:gravity">center</item>