如何更改动态创建的视图的位置

时间:2014-11-20 16:27:37

标签: android button position

我有一个带动作栏的MainActivity。点击" plus",打开一个由EditText,一个按钮"取消"形成的DialogFragment。和一个按钮"继续"。 通过单击"继续",我创建一个新按钮,其中包含我在EditText中放置的字符串。默认按钮位于主活动的左上角。我想要的是每次我创建一个位于右上方的按钮,一个位于另一个下方。我该怎么办?

这是我的代码:

MainActivity

public class MainActivity extends FragmentActivity implements IProjectDialFrag {

    private ProjectDialogFragment projectDialFrag = new ProjectDialogFragment();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        switch (id) {
            case R.id.action_settings:
                //TODO
                return true;
            case R.id.filters:
                Intent intent1 = new Intent(MainActivity.this, FiltersActivity.class);
                startActivity(intent1);
                return true;
            case R.id.action_new:
                return true;
            case R.id.add_button:
                Intent intent2 = new Intent(MainActivity.this, ButtonsActivity.class);
                startActivity(intent2);
                return true;
            case R.id.add_project:
                projectDialFrag.show(getFragmentManager(), "projectDialog");
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }

    }

    @Override
    public void onDialogNegativeClick(DialogFragment dialog) {
        return;
    }

    @Override
    public void onDialogPositiveClick(DialogFragment dialog) { 
        EditText editText = (EditText) dialog.getDialog().findViewById(R.id.project_name);
        String projectName = editText.getText().toString();

        Button projectButton = new Button(this);
            projectButton.setText(projectName);

//      LinearLayout linearLayout = (LinearLayout) findViewById(R.id.main_layout);
//          linearLayout.addView(projectButton);

        RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.main_layout);
            relativeLayout.addView(projectButton);


    }

}

DialogFragment

public class ProjectDialogFragment extends DialogFragment {

    private IProjectDialFrag iProjDialFrag;

    @SuppressLint("InflateParams")
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
         AlertDialog.Builder createProjectAlert = new AlertDialog.Builder(getActivity());

         createProjectAlert.setTitle("Create Project");

         LayoutInflater inflater = getActivity().getLayoutInflater();

         createProjectAlert.setView(inflater.inflate(R.layout.project_dialog_fragment, null))

            .setPositiveButton(R.string.conti_nue, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int id) {
                    iProjDialFrag.onDialogPositiveClick(ProjectDialogFragment.this);
                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int id) {
                    iProjDialFrag.onDialogNegativeClick(ProjectDialogFragment.this);

                }
            });

         return createProjectAlert.create();

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        iProjDialFrag = (IProjectDialFrag) activity;
    }

}

界面

public interface IProjectDialFrag {

    public void onDialogPositiveClick(DialogFragment dialog);
    public void onDialogNegativeClick(DialogFragment dialog);

}

XML布局:

MainActivity布局

<RelativeLayout 

    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="horizontal" >  

</RelativeLayout>

DialogFragment布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <EditText 
        android:id="@+id/project_name" 
        android:inputType="text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginBottom="4dp"
        android:hint="@string/hint_project_alert" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

试试这个......

将MainActivity布局更改为:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 

        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/main_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >  

        <LinearLayout 
            android:id="@+id/buttons_painel" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true">

        </LinearLayout>

    </RelativeLayout>

并更改您的onDialogPositiveClick

@Override
public void onDialogPositiveClick(DialogFragment dialog) { 
    EditText editText = (EditText) dialog.getDialog().findViewById(R.id.project_name);
    String projectName = editText.getText().toString();

    Button projectButton = new Button(this);
        projectButton.setText(projectName);

    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.buttons_painel);
    linearLayout.addView(projectButton);


}