【发布时间】:2015-10-17 11:55:12
【问题描述】:
嘿,我真的需要你的帮助。
我的问题是 Android Studio 无法在模拟器或物理设备中正确显示布局。 每当我放置一个 textView、按钮等并且我想将它居中(水平、垂直或两者),然后我启动模拟器时,它就会卡在左上角。
这是我的代码::
private void firstStage(){
ViewGroup container = (ViewGroup) findViewById(R.id.container);
container.removeAllViews();
container.addView(getLayoutInflater().inflate(R.layout.first_stage, null));
}
private void showHint(){
ViewGroup container = (ViewGroup) findViewById(R.id.container);
container.removeAllViews();
container.addView(getLayoutInflater().inflate(R.layout.first_hint, null));
firstImage = (ImageView) findViewById(R.id.hintImage);
firstImage.setImageResource(R.drawable.firsthint);
Button back = (Button) findViewById(R.id.first_hint_backButton);
back.setOnClickListener(this);
}
在 showHint 方法中,一切正常。按钮和图像视图在我设置它们的地方。
然而,在 firstStage 方法中,我有一个 textView,如果我选择带有“中心”的东西,它总是在左上角,比如 Android Studio 突然无法处理重力:中心了。
我尝试创建一个新项目,但仍然出现错误。如果我将 textView 设置在右侧,然后将其放在中间并带有 margin-right,它就可以工作。但这并不是真正的长期解决方案。有没有人遇到过同样的问题?
在你问之前,是的,我已经用“中心”尝试了一切。在 XML- 文件中,在代码中等等。
也许这对你也有帮助::
如果我通过 setContentView(R.layout.first_stage); 调用布局;有用。 textView 就在我想要的位置.. 我只是不知道为什么它不再适用于 ViewGroup,当它完美地适用于我的其他 2 个布局时?!
这是我的布局代码::
<?xml version="1.0" encoding="utf-8"?>
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/first_stage"
android:id="@+id/textView2"
android:enabled="true"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
/相对布局
编辑::
现在可以了!!我将容器从 RelativeLayout 更改为 FrameLayout。我不知道为什么它现在有效,但它有效!!!
感谢 pdegand59 的快速帮助,非常感谢。
【问题讨论】:
标签: android css android-layout layout