如何在 Java 中并排显示两个 TextView ? 我在 xml 中成功做到了!
我的代码:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12dip"
android:layout_marginRight="10dp"
android:layout_alignParentLeft="true"
android:textColor="#ffffff" />
<TextView
android:id="@+id/value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12dip"
android:layout_alignParentRight="true"
android:textColor="#ffffff" />
</RelativeLayout>
最佳答案
在我的activity_layout
中创建一个LinearLayout
。
LinearLayout lm=(LinearLayout) findViewById(R.id.Linearlayout1);
动态地使 LinearLayout
类型为水平类型。
LinearLayout llh=new LinearLayout(context);
llm.setOrientation(LinearLayout.HORIZONTAL);
然后动态创建了两个TextView
TextView tv1=new TextView(context);
TextView tv2=new TextView(context);
最后将这两个 TextView
添加到水平 LinearLayout
(我们动态创建的),然后将相同的布局添加到 xml 布局。
llh.addView(tv1);
llh.addView(tv2);
lm.addView(llh);
希望对您有所帮助。
关于java - Android如何以编程方式并排设置2个TextView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30302693/