Android RelativeLayout放置元素whith样式

时间:2012-12-16 20:48:38

标签: android styles relativelayout

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

首先我尝试使用static 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);

但我看到元素重叠,因为不接受对齐样式......是否可以使用样式进行此类过程?

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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>