我的xml如下所示我想以编程方式将图像视图和文本视图添加到给定的xml。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="25dip" >
<TextView
android:id="@+text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dip"
android:text="@string/app_name"
android:textSize="24.5sp" />
</LinearLayout>
但我希望以编程方式输出如下:即线性布局包含图像视图和旧文本视图,并将下面的linearlayout添加到新的textview。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="25dip" >
<TextView
android:id="@+name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dip"
android:text="@string/app_name"
android:textSize="24.5sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="25dip" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_marginBottom="12dip"
android:src="@drawable/app_icon_big" />
<TextView
android:id="@+text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dip"
android:text="@string/app_name"
android:textSize="24.5sp" />
</LinearLayout>
</LinearLayout>
答案 0 :(得分:4)
请尝试这种方式,希望这有助于您解决问题。
<强> main.xml中强>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/outerLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="25dp" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:text="@string/app_name"
android:textSize="24.5sp" />
</LinearLayout>
<强> MainActivity.java 强>
public class MyActivity extends Activity {
private LinearLayout outerLinearLayout;
private TextView text1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
outerLinearLayout = (LinearLayout) findViewById(R.id.outerLinearLayout);
text1 = (TextView) findViewById(R.id.text1);
LinearLayout innerLinearLayout = new LinearLayout(this);
ImageView imageView = new ImageView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.bottomMargin=12;
imageView.setLayoutParams(params);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setImageResource(R.drawable.ic_launcher);
innerLinearLayout.addView(imageView);
TextView textView = new TextView(this);
textView.setTextSize((float) 24.5);
textView.setText("New Text View");
textView.setLayoutParams(params);
outerLinearLayout.removeAllViews();
innerLinearLayout.addView(text1);
outerLinearLayout.addView(textView);
outerLinearLayout.addView(innerLinearLayout);
}
}
答案 1 :(得分:0)
更改内部布局
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="25dip" >
通过
<LinearLayout
android:id="@+id/internal_image_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="25dip"
android:visibility="gone" >
然后当你想要展示
时findViewById(R.id.internal_image_textView).setVisibility(View.VISIBLE)