2014-05-02 82 views
-4

佈局\ layout_main.xml的Android佈局編程

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/rR1MessageBubble" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <RelativeLayout 
     android:id="@+id/rR2MessageBubble" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/drawable_sky_background_frame"> 

    </RelativeLayout> 

</RelativeLayout> 

佈局\ layout_component.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="50dp" 
    android:layout_height="50dp" 
    android:orientation="vertical" 
    android:background="@drawable/frame"> 


    <TextView 
     android:id="@+id/txtViewTest" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:text="Hello!" 
     android:gravity="center" /> 

</LinearLayout> 

抽拉\ frame.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > 

    <solid android:color="#FF07DD43" /> 

    <corners android:bottomRightRadius="3dp" 
       android:bottomLeftRadius="3dp" 
       android:topLeftRadius="3dp" 
       android:topRightRadius="3dp"/> 

</shape> 

MainActivity.java

![package com.kore.layoutdemo; 

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 

/** 
* @author ayadav 
* 
*/ 
public class MainActivity extends Activity { 


@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 

    setContentView(R.layout.layout_main); 

    RelativeLayout rR2MessageBubble = (RelativeLayout)findViewById(R.id.rR2MessageBubble); 

    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) rR2MessageBubble.getLayoutParams(); 

    //View-1 
    View view1 = ((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.layout_component, null); 

    rR2MessageBubble.addView(view1); 

    //View-2 
    View view2 = ((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.layout_component, null); 

    TextView txtViewTest = (TextView) view2.findViewById(R.id.txtViewTest); 
    txtViewTest.setText("World"); 

    //params.addRule(RelativeLayout.RIGHT_OF, view1.getId()); 
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 1); 
    view2.setLayoutParams(params); 

    rR2MessageBubble.addView(view2); 

} 

} 

輸出

enter image description here

要求 所有我想補充layout_component.xml的多個實例layout_main.xml所以不是消極投票請建議我通過我能做到這一點的方式。 ..感謝

+0

爲什麼負面投票...所有我想在layout_main.xml中添加layout_component.xml的多個實例,所以而不是負面投票,請建議我通過我可以實現這一點......謝謝 –

回答

2

你犯了一個小錯誤,哥們,你分配一個錯誤LayoutParamview2

更換,

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) rR2MessageBubble.getLayoutParams(); 

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100,100); //or whatever width, height as per your need. 

希望你能理解你的錯誤。

+0

@@斷路器非常感謝你...而不是負面投票你想出解決方案:) –

+0

@@斷路器對不起再次打擾你下面一個有同樣的問題params.addRule(RelativeLayout.BELOW,view1.getId()) ; –

+0

view1.setId(8);事先擺脫我:) –

2

可能是因爲你設置文本爲「世界」。

TextView txtViewTest = (TextView) view2.findViewById(R.id.txtViewTest); 
txtViewTest.setText("World"); 
+0

\ @markieo所有你的意思說view1和view2指向相同的參考? –