使用代码使用相对布局时出错

时间:2015-07-18 11:53:09

标签: java android android-layout

使用代码使用相对布局时,我得到java.lang.IllegalStateException。我知道如何使用xml使用相对布局,但我必须仅使用代码来使用它。

这是我的代码:

package com.example.pr;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnFocusChangeListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class ProfessionalProfileTab extends Fragment
{
    EditText regNumber,medCouncil,languages,degree,course,year,speciality,
    experience_start_month,experience_end_month,designation,experience_company;
    TextView regNumberText,medCouncilText,languagesText,degreeText,specialityText,experienceText;
    ImageButton addDegree;
    Button updateProfession;
    Display display;
    double width,height;
    private int mYear, mMonth, mDay;
    Context con;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        display = getActivity().getWindowManager().getDefaultDisplay();
        width =  MainActivity.Parentwidth;
        height = MainActivity.Parentheight;
        Log.d("Inside Professional Profile Fragment","ppsg");
        Log.d("width",String.valueOf(width));
        Log.d("height",String.valueOf(height));          
    }
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        final View view = inflater.inflate(R.layout.professional_profile_tab, container, false);
        con = container.getContext();
        final LinearLayout layout=(LinearLayout)view.findViewById(R.id.professionalProfileDetails); 
        final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                 android.widget.LinearLayout.LayoutParams.MATCH_PARENT,
        final RelativeLayout rlayout=(RelativeLayout)view.findViewById(R.id.reldegree) ;
        final RelativeLayout.LayoutParams rparam=new RelativeLayout.LayoutParams(
                android.widget.RelativeLayout.LayoutParams.MATCH_PARENT, 
                android.widget.RelativeLayout.LayoutParams.MATCH_PARENT) ;


        final TextView degreeText=new TextView(con);
        degreeText.setTag("degreeText");
        degreeText.setText("degree:");
        layout.addView(degreeText);
        rlayout.addView(degreeText);
        rparam.addRule(RelativeLayout.BELOW,degreeText.getId());
        degreeText.setLayoutParams(params);
        degreeText.setLayoutParams(rparam);

        final ImageButton addDegree=new ImageButton(con);
        addDegree.setTag("addDegree"); 
        addDegree.setImageResource(R.drawable.ic_launcher);
        layout.addView(addDegree);
        rlayout.addView(addDegree);
        rparam.addRule(RelativeLayout.BELOW,addDegree.getId());
        degreeText.setLayoutParams(params);
        addDegree.setLayoutParams(rparam);


        return view;
    }           
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        int id = item.getItemId();
        if (id == R.id.action_settings)
            return true;
        return super.onOptionsItemSelected(item);
    }
}



Here is the xml version of what I want from my code:






<LinearLayout 
        android:id="@+id/professionalProfileDetails"
        android:layout_width="320dp"
        android:layout_height="200dp"
        android:orientation="vertical">
    <RelativeLayout
        android:id="@+id/reldegree" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
            <TextView 
                android:id="@+id/degreeText"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:text="Degree Details"/>
            <ImageButton 
                android:id="@+id/addDegree"
                android:layout_toRightOf="@id/degreeText"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/ic_launcher"/>
      </RelativeLayout>
    </LinearLayout>

这里我想将图像设置在degreetext textField的右侧。     我试图寻找这个问题的解决方案,但找不到任何解决方案。     我们可以像使用xml一样使用相对布局,并使用文本或图像代码设置布局:toRightOf或toLeftOf或以下等。

2 个答案:

答案 0 :(得分:0)

您正尝试将相同的View添加到2个不同的ViewGroup,这是不可能的。

layout.addView(degreeText);
rlayout.addView(degreeText);

此外,您正试图将View置于其下方。

rparam.addRule(RelativeLayout.BELOW,degreeText.getId());
degreeText.setLayoutParams(rparam);

答案 1 :(得分:0)

MyProject-Swift.h