gpt4 book ai didi

android - 无法将项目动态添加到 RelativeLayout 一个低于另一个

转载 作者:行者123 更新时间:2023-11-29 21:51:28 25 4
gpt4 key购买 nike

我在为动态创建的 relativeLayout 添加项目时遇到问题。我看不到添加到其中的第一个元素。每次我只能看到添加到其中的最后一个元素。
我在这里提供了这个邮件 java 源代码和 xml 文件。请帮我解决这个问题:

 DynamicRelativeLayoutActivity.java
==================================

package com.andr.rlayout;



import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class DynamicRelativeLayoutActivity extends Activity {
/** Called when the activity is first created. */
RelativeLayout rLayout;
ScrollView sview;
RelativeLayout dynamiclayout;
LinearLayout horizontalllayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rlayout);
sview = (ScrollView)findViewById(R.id.slayout);
dynamiclayout = new RelativeLayout(DynamicRelativeLayoutActivity.this);
dynamiclayout.setBackgroundColor(Color.WHITE);
sview.addView(dynamiclayout);
RelativeLayout.LayoutParams lp_btn;
horizontalllayout = new LinearLayout(DynamicRelativeLayoutActivity.this);
horizontalllayout.setOrientation(android.widget.LinearLayout.HORIZONTAL);

TextView tv = new TextView(DynamicRelativeLayoutActivity.this);
tv.setText("Hi");
horizontalllayout.addView(tv);
lp_btn = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
lp_btn.addRule(RelativeLayout.BELOW);
dynamiclayout.addView(horizontalllayout, lp_btn);


horizontalllayout = new LinearLayout(DynamicRelativeLayoutActivity.this);
horizontalllayout.setOrientation(android.widget.LinearLayout.HORIZONTAL);
TextView tv1 = new TextView(DynamicRelativeLayoutActivity.this);
tv1.setText("Hello");
horizontalllayout.addView(tv1);
lp_btn = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
lp_btn.addRule(RelativeLayout.BELOW);
dynamiclayout.addView(horizontalllayout, lp_btn);


}
}

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



AndroidManifest.xml
-==================
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.andr.rlayout"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="10" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".DynamicRelativeLayoutActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

最佳答案

//添加id到你的水平布局 View

horizontalllayout.setId(100);

//然后调用

lp_btn.addRule(RelativeLayout.BELOW,horizontalllayout.getId());

//最后添加到你的 ScrollView

sview.addView(dynamiclayout);

关于android - 无法将项目动态添加到 RelativeLayout 一个低于另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14304890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com