我想對齊以權相對佈局內編程問題:的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