回答
只是一个基本的例子:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
Button button1;
button1.setLayoutParams(params);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.RIGHT_OF, button1.getId());
Button button2;
button2.setLayoutParams(params);
正如你所看到的,这是你必须做的事情:
- 创建
RelativeLayout.LayoutParams
对象。 - 使用
addRule(int)
或addRule(int, int)
来设置规则。第一种方法用于添加不需要值的规则。 - 将参数设置为视图(在本例中为每个按钮)。
这里有几个问题。首先我没有看到你实际上在哪里实例化button1或button2。其次,动态声明的Views(ImageViews,Buttons等)以id为-1来实例化。 -1的id对于规则不起作用。 – 2011-12-06 17:38:08
@Cristian:RelativeLayout.LayoutParams和LayoutParams有什么不同?有什么办法可以缩短? – Ci3 2012-11-16 20:45:08
没有像LayoutParams这样的东西。基类实际上是'ViewGroup.LayoutParams'。如果你想缩短它,只需添加一个包含'RelativeLayout.LayoutParams'的导入。 – Cristian 2012-11-17 17:13:31
事情是这样的..
RelativeLayout linearLayout = (RelativeLayout) findViewById(R.id.widget43);
// ListView listView = (ListView) findViewById(R.id.ListView01);
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// View footer = inflater.inflate(R.layout.footer, null);
View footer = LayoutInflater.from(this).inflate(R.layout.footer,
null);
final RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);
footer.setLayoutParams(layoutParams);
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams labelLayoutParams = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
layout.setLayoutParams(labelLayoutParams);
// If you want to add some controls in this Relative Layout
labelLayoutParams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
labelLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
ImageView mImage = new ImageView(this);
mImage.setBackgroundResource(R.drawable.popupnew_bg);
layout.addView(mImage,labelLayoutParams);
setContentView(layout);
你怎么样只是从视图本身拉布局PARAMS如果你创造了它。
$((RelativeLayout)findViewById(R.id.imageButton1)).getLayoutParams();
我希望下面的代码会有所帮助。它将创建一个EditText和一个登录按钮。两者都相对放置。全部在MainActivity.java中完成。
package com.example.atul.allison;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RelativeLayout;
import android.widget.Button;
import android.graphics.Color;
import android.widget.EditText;
import android.content.res.Resources;
import android.util.TypedValue;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Layout
RelativeLayout atulsLayout = new RelativeLayout(this);
atulsLayout.setBackgroundColor(Color.GREEN);
//Button
Button redButton = new Button(this);
redButton.setText("Log In");
redButton.setBackgroundColor(Color.RED);
//Username input
EditText username = new EditText(this);
redButton.setId(1);
username.setId(2);
RelativeLayout.LayoutParams buttonDetails= new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
RelativeLayout.LayoutParams usernameDetails= new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
//give rules to position widgets
usernameDetails.addRule(RelativeLayout.ABOVE,redButton.getId());
usernameDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);
usernameDetails.setMargins(0,0,0,50);
buttonDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);
buttonDetails.addRule(RelativeLayout.CENTER_VERTICAL);
Resources r = getResources();
int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200,r.getDisplayMetrics());
username.setWidth(px);
//Add widget to layout(button is now a child of layout)
atulsLayout.addView(redButton,buttonDetails);
atulsLayout.addView(username,usernameDetails);
//Set these activities content/display to this view
setContentView(atulsLayout);
}
}
- 1. 设置布局PARAMS
- 2. 如何在其他布局之间放置布局在RelativeLayout中?
- 3. 在一个空的RelativeLayout中布局XML
- 4. 在java代码中设置RelativeLayout
- 5. 如何在Android的RelativeLayout类中充气XML布局?
- 6. 错误布局PARAMS设置ImageView的
- 7. 如何在下面的xml布局的relativeLayout
- 8. 布局下在RelativeLayout的
- 9. 如何在代码中设置视图的布局列(视图在表格内)?
- 10. 在运行时在代码中设置布局属性
- 11. 如何在android中设置此布局?
- 12. 如何在xml布局中设置背景颜色?
- 13. 如何使用布局PARAMS
- 14. 如何设置Android的表格布局列填充在XML
- 15. 如何在使用制表符时设置新的xml布局
- 16. 在ViewPagerIndicator中设置布局?
- 17. 如何通过java代码在android中将布局中心设置为垂直?
- 18. XML布局设计和代码不匹配
- 19. 在代码隐藏中设置子布局的路径属性不起作用
- 20. 如何在Android中设置不同类的背景/布局
- 21. 如何在RecycleLayout中设置不同的布局模式?
- 22. UITextViewDelegate:如何在代码中设置代理(不在IB中...)
- 23. 在布局的OnTouchListener的RelativeLayout上的ListView
- 24. 如何在Android中调整RelativeLayout或任何其他布局?
- 25. 当从XML设置时颜色设置不正确 - 在代码
- 26. Android:如何设置上下文到布局xml中的视图?
- 27. Android:如何在线性布局内设置网格布局?
- 28. grails如何迭代params并设置值
- 29. 如何在Android上的xml布局文件中设置WebView上的url?
- 30. 在java代码中的Android布局
的可能的复制[如何铺陈在RelativeLayout的浏览程序?(http://stackoverflow.com/questions/2305395/how-to-lay-out-views-in-relativelayout-programmatically) – 2017-01-19 22:20:50