在我第一次尝试以编程方式创建layout
时,我创建了下面发布的first-xml文件,我想以编程方式将其附加到下面发布的第二个xml文件中。
我还提到了this tutorial,但似乎以编程方式创建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>
second-xml&#34;我希望以编程方式实现&#34;:
<RelativeLayout
android:id="@+id/RelativeLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center_horizontal">
<TextView
android:id="@+id/TextView01"
android:layout_height="wrap_content"
android:text="@string/menu"
android:textSize="@dimen/titlesTextSize"
android:layout_marginLeft="18dp"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="10"
android:layout_width="wrap_content"
android:shadowColor="@android:color/white"
android:textColor="@color/titleColor">
</TextView>
</RelativeLayout>
答案 0 :(得分:1)
尝试以下方法。我没有测试过。如果您有任何问题,请告诉我。
定义像这样的样式
<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>
在java文件中
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);
答案 1 :(得分:0)
// 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);
}
}
答案 2 :(得分:0)
LinearLayout ll = (LinearLayout)findViewById(R.id.my_linearlayout_in_first_xml);
View view = View.inflate(this, R.layout.second_xml, null);
ll.addView(view);