有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java RelativeLayout不会调整大小

package io.github.neelkamath.happylist;

import 安卓.content.Context;
import 安卓.content.SharedPreferences;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.text.Editable;
import 安卓.text.TextWatcher;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.CheckBox;
import 安卓.widget.EditText;
import 安卓.widget.RadioButton;
import 安卓.widget.RelativeLayout;
import 安卓.widget.TextView;

import java.util.Set;

public class MainActivity extends AppCompatActivity {

    int idCount; // Used to give unique IDs to Views.
    EditText newActivityEditText;
    SharedPreferences neededSharedPref;
    SharedPreferences wantedSharedPref;

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

        idCount = 0;
        newActivityEditText = findViewById(R.id.newActivityEditText);
        neededSharedPref = getSharedPreferences("needed", Context.MODE_PRIVATE);
        wantedSharedPref = getSharedPreferences("wanted", Context.MODE_PRIVATE);

        newActivityEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                boolean isExisting = false;
                if (neededSharedPref.contains(charSequence.toString())
                        || wantedSharedPref.contains(charSequence.toString())) {
                    isExisting = true;
                }
                Button create = findViewById(R.id.createButton);
                create.setText(isExisting ? R.string.exists : R.string.create);
                create.setEnabled(!charSequence.toString().equals("") || isExisting);
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });

        addActivities(true);
        addActivities(false);
    }

    public void createActivity(View view) {
        boolean isNeeded = ((RadioButton) findViewById(R.id.neededRadioButton)).isChecked();
        SharedPreferences.Editor e = isNeeded ? neededSharedPref.edit() : wantedSharedPref.edit();
        e.putBoolean(newActivityEditText.getText().toString(), false);
        e.apply();
        newActivityEditText.setText("");
    }

    void addActivities(boolean isNeeded) {
        SharedPreferences sharedPreferences = isNeeded ? neededSharedPref : wantedSharedPref;
        Set<String> strings = sharedPreferences.getAll().keySet();
        int id = isNeeded ? R.id.neededRelativeLayout : R.id.wantedRelativeLayout;
        RelativeLayout layout = findViewById(id);
        TextView prevTextView = null;
        CheckBox prevCheckBox = null;
        for (String s : strings) {
            TextView textView = new TextView(this);
            textView.setText(s);
            textView.setId(++idCount);
            RelativeLayout.LayoutParams txtParams = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT
            );
            txtParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

            CheckBox checkBox = new CheckBox(this);
            checkBox.setChecked(sharedPreferences.getBoolean(s, false));
            checkBox.setId(++idCount);
            RelativeLayout.LayoutParams checkBoxParams = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT
            );
            checkBoxParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

            if (prevTextView != null && prevCheckBox != null) {
                txtParams.addRule(RelativeLayout.BELOW, prevTextView.getId());
                checkBoxParams.addRule(RelativeLayout.BELOW, prevCheckBox.getId());
            }
            layout.addView(textView, txtParams);
            layout.addView(checkBox, checkBoxParams);
            prevTextView = textView;
            prevCheckBox = checkBox;
        }
    }
}

addActivities方法添加了一个RelativeLayout,但是当我向布局中添加新的TextView时,它不会调整大小。我也尝试过自己设置布局的高度,但我找不到计算高度的数据(只有TextView.getLayoutParams().getLineHeight()返回一个正整数,而这不是整个TextView的实际高度)。我找了两个小时,什么也没找到


共 (0) 个答案