问题
The xml file is,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/profileLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Super Powers"
/>
</LinearLayout>
and in Activity file i am trying to add one text feild whose value will change dynamically (value will be either ON or OFF) and that text should be inline with "Super Powers"
TextView valueTV = new TextView(this);
valueTV.setText("OFF");
valueTV.setId(5);
valueTV.setGravity(Gravity.RIGHT);
valueTV.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
((LinearLayout) linearLayout).addView(valueTV);
can anybody tell me how to make two textViews inline?
回答1:
Your LinearLayout has vertical orientation. It should be horizontal to be able to place its children in the same line.
回答2:
try to change the layout to 'relative layout', and then play with the XML
回答3:
Please change your orientation from vertical to horizontal. Now your textviews will be oriented horizontally.
回答4:
use this code for designing xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Super power"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:id="@+id/lin1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
and add your dynamic text view in LinearLayout whoes id is "lin1" hope it will work for you
回答5:
Try this way :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Super Powers : "
/>
<TextView
android:id="@+id/txt_on_off"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OFF" />
</LinearLayout>
</LinearLayout>
and in your java file take the reference of ON OFF textview like this :
TextView txtOnOff;
txtOnOff=(TextView)findViewById(R.id.txt_on_off);
txtOnOff.setText("ON");
thats all
来源:https://stackoverflow.com/questions/24931904/not-able-to-align-two-text-views-on-the-same-line-in-android