【问题标题】:Android - RelativeLayout CENTER_VERTICAL and BELOW issueAndroid - RelativeLayout CENTER_VERTICAL 和 BELOW 问题
【发布时间】:2011-03-20 15:09:46
【问题描述】:

我想要一个TextView 在上面,一个VideoView 在它下面。我想将VideoView 作为中心垂直并在TextView 下方。我正在使用RelativeLayout

我正在尝试在代码中执行此操作:

RelativeLayout layout = new RelativeLayout(this);
TextView tv = new TextView(this);
tv.setText("Hello");
tv.setGravity(Gravity.RIGHT);
tv.setTextColor(Color.WHITE);
tv.setId(1);

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

one.addRule(RelativeLayout.ALIGN_PARENT_TOP);
one.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);


RelativeLayout.LayoutParams two = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);  
two.addRule(RelativeLayout.CENTER_VERTICAL); 
two.addRule(RelativeLayout.BELOW, 1);

VideoView mVideoView = new VideoView(this);

layout.addView(tv, one);
layout.addView(mVideoView, two);

setContentView(layout);

VideoView 位于TextView 下方,但VideoView 不是垂直居中。

【问题讨论】:

    标签: android center android-relativelayout


    【解决方案1】:

    您制作了 VideoView fill_parent/fill_parent,这使其具有与其父 RelativeLayout 相同的尺寸。如果它和RelativeLayout 一样大,那么居中就不能真正起作用。此外,您不能让 VideoView 既位于 TextView 下方,又在父级中垂直居中;这是一个或另一个。相反,您可以将 ViewView 居中并将 TextView 放在其上方。

    【讨论】:

    • 我将fill_parent/fill_parent 用于videoview 的原因是我希望视频填满TextView 下面的所有空间。视频只会放大到其宽高比允许的范围,这就是为什么需要居中,尤其是在纵向模式下。
    猜你喜欢
    • 1970-01-01
    • 2011-09-28
    • 2011-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多