我想对齐以权相对布局内编程问题:的Android的TextView不对齐到右的RelativeLayout
这里是XML(我不蚂蚁改变主布局,我想它的线性布局,因为我'模拟题):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main2"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.upf.ctrl_tp2.Main2Activity"
android:orientation="vertical">
</LinearLayout>
这里是mainActivity.java代码:
public class Main2Activity extends AppCompatActivity {
LinearLayout main;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
main = (LinearLayout) findViewById(R.id.activity_main2);
TextView txt = new TextView(this);
txt.setText("Hello World!");
txt.setTextSize(20);
RelativeLayout r = new RelativeLayout(this);
RelativeLayout.LayoutParams laypar = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
laypar.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);//seems that this doesn't work ?
r.setLayoutParams(laypar);
r.addView(txt);
main.addView(r);
}}
我得到的结果:
我想要的东西:
要在相对布局或线性布局 – Keerthivasan
采用Android对齐:重力=“权“为你的TextView。你需要相关布局 –
你的textview是在relativelayout里面的,对吧?尝试设置'ALIGN_PARENT_RIGHT'到文本视图,而不是它的视图。 – DrNachtschatten