0

对不起,如果我问愚蠢的问题,但到目前为止还没有弄清楚这个问题。我有一个带有 2 个单选按钮的表单,它们决定了布局并使用了它们下面的布局组件。这是其中一种布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
    <TextView
            android:id="@+id/arrival_date_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/arrival_date_label"/>
    <EditText
            android:id="@+id/arrival_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/arrival_date_label"
            android:hint="10/05/2012"/>
    <Spinner
            android:id="@+id/arrival_time_spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/arrival_date"/>
    <TextView
            android:id="@+id/return_date_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/return_date_label"
            android:layout_below="@id/arrival_time_spinner"/>
    <EditText
            android:id="@+id/return_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/return_date_label"
            android:hint="10/05/2012"/>
    <Spinner
            android:id="@+id/return_time_spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/return_date"/>
</RelativeLayout>

和 Java 类来初始化一些使用的组件


public class ReturnFixedDays extends RelativeLayout {
    private EditText arrivalDate;
    private Spinner arrivalTime;
    private EditText returnDate;
    private Spinner returnTime;
    private final String pattern = "dd/MM/yyyy";

    public ReturnFixedDays(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.return_fixed_days, null);

        arrivalDate = (EditText) view.findViewById(R.id.arrival_date);
        arrivalDate.setText(arrivalDate());

        arrivalTime = (Spinner) view.findViewById(R.id.arrival_time_spinner);
        ArrayAdapter adapter = new ArrayAdapter(context, R.array.travel_time_entries,android.R.layout.simple_spinner_item);
        arrivalTime.setAdapter(adapter);

        returnDate = (EditText) view.findViewById(R.id.return_date);
        returnDate.setText(returnDate());

        returnTime = (Spinner) view.findViewById(R.id.return_time_spinner);
        ArrayAdapter adapter2 = new ArrayAdapter(context, R.array.travel_time_entries,android.R.layout.simple_spinner_item);
        returnTime.setAdapter(adapter2);
    }

    private String arrivalDate(){
        return dateToString(today());
    }

    private String returnDate(){
        return dateToString(weekLater());
    }

    private String dateToString(Date date){
        return formatter().format(date);
    }

    private SimpleDateFormat formatter(){
        return new SimpleDateFormat(pattern);
    }

    private Date weekLater(){
        return adjustDateBy(today(), 7);
    }

    private Date today(){
        return Calendar.getInstance().getTime();
    }

    private Date adjustDateBy(Date currentDate, int numOfDays) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        calendar.add(Calendar.DATE, numOfDays);

        return calendar.getTime();

    }

}

现在要添加此布局以查看我在 xml 中执行以下操作

<RelativeLayout
            android:id="@+id/dates_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/flight_layout">
        <TextView
                android:id="@+id/date_label"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="@string/dates_label"
                style="@style/Label"/>
        <RadioGroup
                android:id="@+id/dates_radio_group"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_below="@id/date_label">
            <RadioButton
                    android:id="@+id/flexible_dates_radio"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/flexible_dates_label"
                    style="@style/Label"/>
            <RadioButton
                    android:id="@+id/fixed_dates_radio"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:text="@string/fixed_dates_label"
                    style="@style/Label"/>
        </RadioGroup>
    </RelativeLayout>
    <RelativeLayout
            android:id="@+id/stub_container"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/dates_layout">

        <com.flyweekend.android.date.view.ReturnFixedDays
                android:id="@+id/return_fixed_days_stub"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout="@layout/return_fixed_days"/>

        <com.flyweekend.android.date.view.ReturnFlexibleDays
                android:id="@+id/return_flexible_days_stub"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout="@layout/return_flexible_days"/>
    </RelativeLayout>

FlightView.java


@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        context = getActivity();
        dateTypeRadioGrp = dateTypeRadioGroup(view);
        returnFixedDays = (RelativeLayout) view.findViewById(R.id.return_fixed_days_stub);
        updateDatesVisibility();
        return view;
    }

private RadioGroup dateTypeRadioGroup(View view){
        RadioGroup group = (RadioGroup)view.findViewById(R.id.dates_radio_group);
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup radioGroup, int selected) {
                if(selected == R.id.flexible_dates_radio){
                    Log.i(TAG, "Flexible dates selected");
                    updateDatesVisibility();
                }else{
                    Log.i(TAG, "Fixed dates selected");
                    updateDatesVisibility();
                }
            }
        });
        return group;
    }

    private void updateDatesVisibility(){
        if(dateTypeRadioGrp.getCheckedRadioButtonId() == R.id.fixed_dates_radio){
            returnFixedDays.setVisibility(View.VISIBLE);
        } else{
            returnFixedDays.setVisibility(View.INVISIBLE);
        }
    }

然而,这两个群体都不可见。我想我需要额外的一双眼睛来发现我在动态添加这些布局时出错的地方。

4

1 回答 1

1

有了这个:

View view = inflater.inflate(R.layout.return_fixed_days, null);

您刚刚膨胀了一个布局文件,实际上并没有将它附加到您的自定义视图ReturnFixedDays中。要将膨胀的布局附加到您的自定义视图,您必须this作为第二个参数传递(代表新膨胀的视图层次结构的父级):

View view = inflater.inflate(R.layout.return_fixed_days, this);

您的代码中可能还有其他问题,因此请检查并查看这是否解决了您的问题。

于 2012-05-14T11:41:14.187 回答