我的MainActivity
最初为空,带有操作栏。点击"加"在操作栏中,我打开一个DialogFragment
,其中包含EditText
和两个按钮,"取消"和"继续"。我无法发布图片,因为我没有足够的声誉,但是当我点击"继续"我创建了两个按钮;一个位于MainActivity
布局的右上角,另一个位于前一个布局的左下方。我想要的是每次点击"继续",我可以根据我之前解释的相同模式创建两个按钮。
这是我编写的代码,但它不能按照我的意愿运作。
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);
ImageButton playButton = new ImageButton(this);
playButton.setImageResource(R.drawable.ic_action_play_over_video);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.button_row);
linearLayout.addView(projectButton);
linearLayout.addView(playButton);
}
}
界面
public interface IProjectDialFrag {
public void onDialogPositiveClick(DialogFragment dialog);
public void onDialogNegativeClick(DialogFragment dialog);
}
和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;
}
}
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" >
<LinearLayout
android:id="@+id/button_row"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" >
</LinearLayout>
</RelativeLayout>
如果您需要截图,请给我一点声望,我会立即放置。比你
再次感谢你。这是我的截图:
这是我想要的结果:
对于每个按钮,我必须能够添加更多&#34;播放&#34;。
答案 0 :(得分:0)
但请看你的布局。
您的线性布局在宽度上是“wrap_conent”。 您的线性布局也粘贴在父级的右侧。 所以这正是你将得到的......所有的线性布局内容都粘在右侧并且采用了它的内容的优秀尺寸......好吧按钮。你得到了你告诉Android的内容。
如果你想得到你画出的东西,那么首先你的按钮的线性布局应该是match_parent,你可以将它的重力设置为左边的播放按钮,右边是CIAO和LUCA按钮......
答案 1 :(得分:0)
<LinearLayout
...
android:layout_width="wrap_content"
... >
MainActivity布局中的应更改为
<LinearLayout
...
android:layout_width="fill_parent"
... >
并添加这些行
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT) <---
lp.gravity = Gravity.RIGHT; <---
Button projectButton = new Button(this);
projectButton.setText(projectName);
projectButton.setLayoutParams(lp); <---
lp.gravity = Gravity.LEFT; <---
ImageButton playButton = new ImageButton(this);
playButton.setImageResource(R.drawable.ic_action_play_over_video);
playButton.setLayoutParams(lp); <---
答案 2 :(得分:0)
@ udiboy1209
我只是重构你的代码,如下所示:
@Override
public void onDialogPositiveClick(DialogFragment dialog) {
EditText editText = (EditText) dialog.getDialog().findViewById(R.id.project_name);
String projectName = editText.getText().toString();
LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp1.gravity = Gravity.END;
Button projectButton = new Button(this);
projectButton.setText(projectName);
projectButton.setLayoutParams(lp1);
LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp2.gravity = Gravity.START;
Button playButton = new Button(this);
playButton.setBackgroundResource(R.drawable.play_button_green);
playButton.setLayoutParams(lp2);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.button_row);
linearLayout.addView(projectButton);
linearLayout.addView(playButton);
}