簡體   English   中英

Android如何以編程方式並排設置2 TextView?

[英]Android how to set 2 TextView side by side programmatically?

如何在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代碼,

layout = (LinearLayout)findViewById(R.id.mainLay);

    TextView tv1 = new TextView(ActivityName.this);
    tv1.setText("TextView 1");
    LinearLayout.LayoutParams childParam1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
    childParam1.weight = 0.5f;
    tv1.setLayoutParams(childParam1);

    TextView tv2 = new TextView(ActivityName.this);
    tv2.setText("TextView 2");
    LinearLayout.LayoutParams childParam2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    childParam1.weight = 0.5f;
    tv2.setLayoutParams(childParam2);

    layout.addView(tv1);
    layout.addView(tv2);

和xml代碼,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<LinearLayout
    android:weightSum="1"
    android:id="@+id/mainLay"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"></LinearLayout>
</LinearLayout>

這應該工作。

第一個解決方案:使用在線轉換器[ http://www.xmltojava.com/][1]

第二種解決方案:擴大布局

LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View layoutView = mInflater.inflate(R.rowLayout, null);
TextView tvOne=(TextView) layoutView.findViewById(R.id.tvOne);

這是我嘗試做的

        final LinearLayout ll = (LinearLayout) findViewById(R.id.container_LV);


                    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
                            (
                            RelativeLayout.LayoutParams.MATCH_PARENT,
                            RelativeLayout.LayoutParams.MATCH_PARENT
                            );
                    //ll.setOrientation(orientation);
                    // params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,0);
                        LinearLayout llh=new LinearLayout(getApplicationContext());
                        ll.setOrientation(LinearLayout.HORIZONTAL);

                    for (int i = 0; i < fields.length(); i++) 
                    {

                        final TextView txtLabel = new TextView(getApplicationContext());
                        final TextView txtValue = new TextView(getApplicationContext());

                        jsonLabel = fields.getJSONObject(i).getString(TAG_LABEL) ; //+ "\n";
                        jsonValue = fields.getJSONObject(i).getString(TAG_VALUE) ; //+ "\n";

                        //txtLabel.setBackgroundColor(Color.YELLOW);
                        //txtValue.setBackgroundColor(Color.YELLOW);
                        //txtLabel.setWidth(pixels);

                        txtLabel.setLayoutParams(params);
                        txtValue.setLayoutParams(params);


                        txtValue.setGravity(Gravity.RIGHT);

                        txtLabel.setGravity(Gravity.LEFT);

                        //params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,0);

                        txtLabel.setText(jsonLabel);
                        txtValue.setText(jsonValue);

                        txtLabel.setTextColor(Color.WHITE);
                        txtValue.setTextColor(Color.WHITE);

                        //txtLabel.


                        llh.addView(txtLabel);
                        llh.addView(txtValue);

                    }

                        ll.addView(llh);

關鍵是僅在將android:layout_width設置為"0dp"后才能使用android:layout_weight ,嘗試這個

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView 
    android:id="@+id/label"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textSize="12dip"
    android:layout_marginRight="10dp"
    android:layout_alignParentLeft="true"
    android:textColor="#ffffff" />

<TextView 
    android:id="@+id/value"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textSize="12dip"
    android:layout_alignParentRight="true"
    android:textColor="#ffffff" />

</RelativeLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM