1

这是我到目前为止的代码。我应该改变什么,使它看起来像下面的 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 |
-------------------------
4

2 回答 2

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);

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

于 2013-07-17T02:06:00.447 回答
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);
于 2013-07-17T00:38:26.350 回答