我想在运行时以编程方式将视图的位置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);
}
}
答案 0 :(得分:0)
当你忽视最简单的事情时会发生什么。我忘了在每个案例的最后都加上破解声明。