在运行时期间无法正确更新RelativeLayout位置

时间:2017-05-27 04:44:47

标签: android android-layout relativelayout

我想在运行时以编程方式将视图的位置Relative更改为其父级。我创建了Spinner来选择和更改TextView的位置。我的策略是重置所有可能的规则(例如params.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0)),然后添加我选择所需的必要规则。但是,除了三种情况外,TextView已被放置在错误的位置。以下是选择和回复:

  • 左上角 - >左上角(正常工作)
  • 热门中心 - >左上角
  • 右上角 - >左上角
  • 左中 - >左下角
  • 中右 - >左下角
  • 左下角 - >左下角(正常工作)
  • 底部中心 - >右下角
  • 右下 - >右下角(正常工作)

我做错了什么?

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="myapps.test.updateposition.MainActivity">
    <Spinner
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:id="@+id/mySpinner"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/helloWorld"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TextView textView = (TextView) findViewById(R.id.helloWorld);
        final Spinner mySpinner = (Spinner) findViewById(R.id.mySpinner);
        List<String> list = new ArrayList<String>();
        //"Top Left","Top Center","Top Right","Middle Left","Middle Right","Bottom Left", "Bottom Center", "Bottom Right"
        list.add("Top Left");
        list.add("Top Center");
        list.add("Top Right");
        list.add("Middle Left");
        list.add("Middle Right");
        list.add("Bottom Left");
        list.add("Bottom Center");
        list.add("Bottom Right");
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mySpinner.setAdapter(dataAdapter);

        mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                setMyParams(textView, mySpinner.getItemAtPosition(position).toString());
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }

    public void setMyParams(TextView textView, String position) {
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) textView.getLayoutParams();
        // Reset all rules
        params.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 0);
        params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
        params.addRule(RelativeLayout.CENTER_HORIZONTAL, 0);
        params.addRule(RelativeLayout.CENTER_VERTICAL, 0);

        switch (position) {
            case "Top Left":
                params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            case "Top Center":
                params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                params.addRule(RelativeLayout.CENTER_HORIZONTAL);
            case "Top Right":
                params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            case "Middle Left":
                params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                params.addRule(RelativeLayout.CENTER_VERTICAL);
            case "Middle Right":
                params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                params.addRule(RelativeLayout.CENTER_VERTICAL);
            case "Bottom Left":
                params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            case "Bottom Center":
                params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                params.addRule(RelativeLayout.CENTER_HORIZONTAL);
            case "Bottom Right":
                params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        }
        textView.setLayoutParams(params);
    }
}

1 个答案:

答案 0 :(得分:0)

当你忽视最简单的事情时会发生什么。我忘了在每个案例的最后都加上破解声明。