2017-04-27 58 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); 
相關問題