2017-04-27 33 views
-1

我想对齐以权相对布局内编程问题:的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); 
}} 

我得到的结果: click to see picture

我想要的东西: click to see picture

+1

要在相对布局或线性布局 – Keerthivasan

+0

采用Android对齐:重力=“权“为你的TextView。你需要相关布局 –

+0

你的textview是在relativelayout里面的,对吧?尝试设置'ALIGN_PARENT_RIGHT'到文本视图,而不是它的视图。 – DrNachtschatten

回答

2

右对齐在线性布局内不起作用。 layout_gravity将起作用。

更换

laypar.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);//seems that this doesn't work ? 

与此

laypar.gravity = Gravity.RIGHT; 
+0

这将使该布局的所有孩子都与右边缘对齐,这不是什么OP已经提出要求(尽管他还没有意识到)。正确的答案似乎由VishnuSP发布。 – azizbekian

+0

解决,谢谢! txt.setLayoutParams(laypar); – CrazyDeveloper

1

设置LayoutParamTextView

txt.setLayoutParams(laypar); 

您还可以在xml中使用android:layout_alignParentRight="true

0

是MR。 @CrazyDeveloper所有的东西都看起来不错,只有你必须要说的一点。

RelativeLayout r = new RelativeLayout(this); 

// actul issue belove . 

RelativeLayout.LayoutParams laypar = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 

只是改变它。

RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 

宽度为MATCH_PARENT,而不是WRAP_CONTENT

+0

固定为: txt.setLayoutParams(laypar); – CrazyDeveloper

0

试试这个:

main = (LinearLayout) findViewById(R.id.ll_layout); 
     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.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
     laypar.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);//seems that this doesn't work ? 
     r.setGravity(Gravity.RIGHT); 
     r.setLayoutParams(laypar); 
     r.addView(txt); 
     ll_layout.addView(r); 
相关问题