0

我想将两个元素排成一行,一个在右边,一个在左边。我想从代码中做到这一点......并使用样式。

首先我用静态 layout.xml 尝试了它:这很好用:

 <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="@string/hello_world" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="TextView" />

 </RelativeLayout>

然后我创造了风格,

   <style name="t1" parent="@android:style/TextAppearance.Large">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_alignParentLeft">true</item>
</style>

<style name="t2" parent="@android:style/TextAppearance.Large">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_alignParentRight">true</item>
</style>   

并尝试:

RelativeLayout rel = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new 
        RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT);
    rel.setLayoutParams(params);

    TextView t1 = new TextView(this);   
    t1.setText("balbla_1");
    t1.setTextAppearance(this, R.style.t1);


    TextView t2 = new TextView(this);
    t2.setText("balbla_2");
    t2.setTextAppearance(this, R.style.t2);


    rel.addView(t1);
    rel.addView(t2);

    setContentView(rel);

但是我看到元素重叠,因为不接受 align 的样式...是否可以在此类过程中使用样式?

4

2 回答 2

1

正如 javadoc 中所指定的,setTextAppearence 仅适用于某些属性:

/**
 * Sets the text color, size, style, hint color, and highlight color
 * from the specified TextAppearance resource.
 */

但是你可以做这样的事情

    TextView t1 = new TextView(this);
    t1.setText("balbla_1");
    t1.setTextAppearance(this, R.style.t1);
    RelativeLayout.LayoutParams t1_params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    t1_params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);


    TextView t2 = new TextView(this);
    t2.setText("balbla_2");
    t2.setTextAppearance(this, R.style.t2);
    RelativeLayout.LayoutParams t2_params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    t2_params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);


    rel.addView(t1, t1_params);
    rel.addView(t2, t2_params);
于 2012-12-16T21:52:39.253 回答
0

你试过线性布局吗?喜欢;

 <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:text="@string/o1" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:text="@string/o2" />
  </LinearLayout>
于 2012-12-16T20:55:38.703 回答