Android Studio Layout Errors

Hey I really need your help.

My problem is that Android Studio will not display the Layout correctly in the emulator or physical device. Whenever i place a textView, button etc. and i want to center it (horizontally, vertically or both), and i start the emulator it's stuck to the top left corner.

This is my code::

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);


}

In the showHint Method everything works fine.. the button and imageview are there where i have set them.

In the firstStage method however i have a textView and it is always in the top left corner if I choose something with "center", like Android Studio suddenly can't handle gravity:center anymore.

I tried it with creating a new project but the error still occurs. If i set the textView to the right side and then put it in the middle with margin-right, it works. But that is not really a long-term solution. Did anyone of you experience the same problem?

And before you ask, Yes i have tried everything with "center". In the XML- file, in the code and so on.

Maybe this will also help you::

If i call the layout via setContentView(R.layout.first_stage); it works. The textView is right in there where i want it to be.. I just don't know why it won't work with ViewGroup anymore, when it works perfectly for my other 2 Layouts?!

This is my Layout-Code::

<?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

EDIT::

It works now!! I changed the container from being a RelativeLayout to a FrameLayout. I don't know exactely why it works now, but it works!!!

Thanks to pdegand59 for the fast help, really appreciate it.


When inflating a view, you have to give a parent to the inflater to resolve the layout params of the view.

container.addView(getLayoutInflater().inflate(R.layout.first_stage, container, false));

That's why Android Studio has a warning when inflating with null parent.

By the way, you can add the inflated view automatically to your view hierarchy with only :

getLayoutInflater().inflate(R.layout.first_stage, container, true);

链接地址: http://www.djcxy.com/p/84752.html

上一篇: 无法解析

下一篇: Android Studio布局错误