以编程方式:顶部的Android ListView和EditText&底部的按钮

时间:2013-07-17 00:07:42

标签: android android-layout

这是我到目前为止的代码。我应该更改什么,以便它看起来像下面的ASCII艺术?

private RelativeLayout mMainLayout;
private LinearLayout mButtonLayout;
private ListView mListView;
private EditText mMessageInput;
private Button mSendButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mMainLayout = new RelativeLayout(this);
    mMainLayout.setLayoutParams(new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.MATCH_PARENT,
    LinearLayout.LayoutParams.MATCH_PARENT));
    setContentView(mMainLayout);
    mMainLayout.setGravity(Gravity.BOTTOM);

    mListView = new ListView(this);
    mMainLayout.addView(mListView, 0);

    mButtonLayout = new LinearLayout(this);
    mButtonLayout.setLayoutParams(new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.MATCH_PARENT,
    LinearLayout.LayoutParams.WRAP_CONTENT, 2f));
    mMessageInput = new EditText(this);
    mMessageInput.setMinimumHeight(200);
    mButtonLayout.addView(mMessageInput, 0);

    mSendButton = new Button(this);
    mSendButton.setText(R.string.send);
    mSendButton.setOnClickListener(this);
    mButtonLayout.addView(mSendButton, 1);
    mMainLayout.addView(mButtonLayout, 1);
}


-------------------------
|ListView               |
|                       |
|                       |
|                       |
|                       |
|                       |
|                       |
|                       |
|                       |
-------------------------
| EditText      |Button |
-------------------------

2 个答案:

答案 0 :(得分:3)

private LinearLayout mMainLayout;
private LinearLayout mButtonLayout;
private ListView mListView;
private EditText mMessageInput;
private Button mSendButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMainLayout = new LinearLayout(this);
mMainLayout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
mMainLayout.setOrientation(LinearLayout.VERTICAL);
setContentView(mMainLayout);
mMainLayout.setGravity(Gravity.BOTTOM);

mButtonLayout = new LinearLayout(this);
mButtonLayout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 2f));
mMessageInput = new EditText(this);
mMessageInput.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
mButtonLayout.addView(mMessageInput, 0);

mSendButton = new Button(this);
mSendButton.setText(R.string.send);
mSendButton.setOnClickListener(this);
mSendButton.setLayoutParams(new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 3f));
mSendButton.setMinHeight(300);
mButtonLayout.addView(mSendButton, 1);
mMainLayout.addView(mButtonLayout, 1);

mListView = new ListView(this);
mListView.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT, 1f));
mMainLayout.addView(mListView, 0);

您应该像上面的代码一样从下到上进行布局。 所以首先在布局中添加底部的两个按钮然后添加列表视图。 我希望它对你有所帮助。

答案 1 :(得分:1)

以下是解决方案:

private LinearLayout mMainLayout;
private LinearLayout mButtonLayout;
private ListView mListView;
private EditText mMessageInput;
private Button mSendButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mMainLayout = new LinearLayout(this);
    mMainLayout.setLayoutParams(new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.MATCH_PARENT));
    mMainLayout.setOrientation(LinearLayout.VERTICAL);
    setContentView(mMainLayout);
    mMainLayout.setGravity(Gravity.BOTTOM);

    mListView = new ListView(this);
    mListView.setLayoutParams(new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.MATCH_PARENT,
    LinearLayout.LayoutParams.MATCH_PARENT, 1f));
    mMainLayout.addView(mListView, 0);

    mButtonLayout = new LinearLayout(this);
    mButtonLayout.setLayoutParams(new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.MATCH_PARENT,
    LinearLayout.LayoutParams.WRAP_CONTENT, 2f));
    mMessageInput = new EditText(this);
    mMessageInput.setLayoutParams(new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.MATCH_PARENT,
    LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
    mButtonLayout.addView(mMessageInput, 0);

    mSendButton = new Button(this);
    mSendButton.setText(R.string.send);
    mSendButton.setOnClickListener(this);
    mSendButton.setLayoutParams(new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
    LinearLayout.LayoutParams.WRAP_CONTENT, 3f));
    mSendButton.setMinHeight(300);
    mButtonLayout.addView(mSendButton, 1);
    mMainLayout.addView(mButtonLayout, 1);