Android 以编程方式创建relativeLayout?

Android 以编程方式创建relativeLayout?,android,xml,android-layout,Android,Xml,Android Layout,在我第一次尝试以编程方式创建布局时,我创建了下面发布的第一个xml文件,并希望以编程方式将下面发布的第二个xml文件附加到该文件中。 我也提到了,但似乎以编程方式创建linearlayout和relativelayout是不同的。另外,在我尝试创建relativelayouteclipse时,我用红色波浪线强调了我的layoutparams,另外,我不知道首先设置的参数是宽度还是高度。请提供建议和建议 我的尝试: protected void onCreate(Bundle savedInsta

在我第一次尝试以编程方式创建
布局时,我创建了下面发布的第一个xml文件,并希望以编程方式将下面发布的第二个xml文件附加到该文件中。
我也提到了,但似乎以编程方式创建
linearlayout
relativelayout
是不同的。另外,在我尝试创建
relativelayout
eclipse时,我用红色波浪线强调了我的
layoutparams
,另外,我不知道首先设置的参数是宽度还是高度。请提供建议和建议

我的尝试:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_layout_programmatically_00);

    RelativeLayout mRelLayout00 = new RelativeLayout(this);
    android.view.ViewGroup.LayoutParams mRelLayoutParams = new  
            LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT
            , android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
第一个xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" 
android:layout_width="match_parent">

      <LinearLayout 
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical">
      </LinearLayout>

第二个xml“我希望以编程方式实现的whta”:


//试试这个方法,希望这能帮助你。。。
**activity_main.xml**
**MainActivity.java**
公共类MainActivity扩展了活动{
私人线路布置图;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lnrMain=(LinearLayout)findViewById(R.id.lnrMain);
RelativeLayout mRelLayout00=新的RelativeLayout(本);
RelativeLayout.LayoutParams mRelLayoutParams=新建
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_父项,RelativeLayout.LayoutParams.WRAP_内容);
mRelLayout00.setLayoutParams(mRelLayoutParams);
TextView TextView=新的TextView(此);
RelativeLayout.LayoutParams textParams=新的RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_内容,RelativeLayout.LayoutParams.WRAP_内容);
textParams.setMargins(18,0,0,0);
setLayoutParams(textParams);
textView.setTextSize(16);
setShadowLayer(10,0,0,android.R.color.white);
setText(“菜单”);
mRelLayout00.addView(textView);
lnrMain.addView(MR00);
}
}

尝试以下操作。我还没有测试过。如果你有任何问题,请告诉我

像这样定义样式

<style name="MyStyleText">
    <item name="android:textColor">#ff0000</item>
    <item name="android:textSize">12sp</item>
    <item name="android:shadowColor">#ffffff</item>
    <item name="android:shadowDx">0</item>
    <item name="android:shadowDy">0</item>
    <item name="android:shadowRadius">10</item>
  </style>

所以你有两个不同的xml文件,你想通过编程将它们都添加到第三个布局中?不,发布的“第一个xml”就是我在布局文件夹中的那个。“第二个xml”的内容是我希望通过编程实现的内容。膨胀第二个xml并添加到relativelayout。我将简短地添加代码我不想使用充气机,然后从头开始创建它。但是,如果在第二个xml中有很多视图,最好使用充气器。这就是它的意思,你能告诉我,如何设置重力和布局重力吗?
// try this way,hope this will help you...

**activity_main.xml**

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <LinearLayout
        android:id="@+id/lnrMain"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    </LinearLayout>
</FrameLayout>

**MainActivity.java**

public class MainActivity extends Activity{

    private LinearLayout lnrMain;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lnrMain = (LinearLayout) findViewById(R.id.lnrMain);

        RelativeLayout mRelLayout00 = new RelativeLayout(this);
        RelativeLayout.LayoutParams mRelLayoutParams = new
                RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        mRelLayout00.setLayoutParams(mRelLayoutParams);
        TextView textView = new TextView(this);
        RelativeLayout.LayoutParams textParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        textParams.setMargins(18,0,0,0);
        textView.setLayoutParams(textParams);
        textView.setTextSize(16);
        textView.setShadowLayer(10,0,0,android.R.color.white);
        textView.setText("menu");
        mRelLayout00.addView(textView);
        lnrMain.addView(mRelLayout00);
    }
}
<style name="MyStyleText">
    <item name="android:textColor">#ff0000</item>
    <item name="android:textSize">12sp</item>
    <item name="android:shadowColor">#ffffff</item>
    <item name="android:shadowDx">0</item>
    <item name="android:shadowDy">0</item>
    <item name="android:shadowRadius">10</item>
  </style>
RelativeLayout.LayoutParams lp ;
    lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);


RelativeLayout.LayoutParams textLP ;
    textLP = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

RelativeLayout rl = new RelativeLayout(this);


TextView tv = new TextView(this);
tv.setText(getString(R.string.hello_world));
tv.setTextAppearance(getApplicationContext(), R.style.MyStyleText);

rl.addView(tv, textLP);
this.addContentView(rl, lp);
LinearLayout ll = (LinearLayout)findViewById(R.id.my_linearlayout_in_first_xml);

View view = View.inflate(this, R.layout.second_xml, null);

ll.addView(view);