开发者

Android: Relative layout issue

开发者 https://www.devze.com 2023-02-05 10:07 出处:网络
My xml: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

My 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="fill_parent">
    <TextView android:id="@+id/Title" android:text="title"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:singleLine="true" android:layout_alignParentTop="true" />
    <EditText android:id="@+id/ReplyText" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:gravity="top"
        android:layout_below="@+id/Title" android:layout_above="@+id/Save" />
<!--    <WebView android:id="@+id/webview" android:layout_width="fill_parent"-->
<!--        android:layout_height="fill_parent" android:layout_belo开发者_StackOverflow社区w="@+id/Title"-->
<!--        android:layout_above="@+id/Save"/>-->
    <Button android:text="Save" android:id="@+id/Save"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

Output in sdk 3 (1.5) and sdk 9 (2.3)

Android: Relative layout issue

Question: Why EditText is not displaying in 1.5 version?

There must be a solution of this, because if I replace EditText with WebView with a little code added in onCreate method:

WebView mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadData("test", "text/html", "utf-8");
mWebView.setWebViewClient(new WebViewClient());

It displays WebView perfectly in both the version.

Goal: I want to set header and footer elements as show in picture and middle element should have full height/width of the rest of the place.


Not sure if this could be the issue, but this is wrong:

android:layout_below="@+id/Title" android:layout_above="@+id/Save"

the @+id means adding a new id, and in this case you are refering to Ids already defined (you define them in android:id="@+id/Title" and android:id="@+id/Save").

Also I had some problems in the past when referencing an id before the component itself (in this case you say in the EditText that it's above the button and the button is defined later)

I would write your whole layout like this:

<?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="fill_parent">

    <TextView android:id="@+id/Title"
        android:text="title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:layout_alignParentTop="true" />
    <Button android:text="Save"
        android:id="@+id/Save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />
    <EditText android:id="@+id/ReplyText"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="top"
        android:layout_below="@id/Title"
        android:layout_above="@id/Save" />

</RelativeLayout>


If the depth layering may be an issue you can also define the XML like this:

<?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="fill_parent">

<TextView android:id="@+id/Title"
    android:text="title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:layout_alignParentTop="true" />

 <EditText android:id="@+id/ReplyText"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="top"
        android:layout_below="@id/Title"
        android:layout_above="@+id/Save" />

    <Button android:text="Save"
        android:id="@id/Save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

Notice that the id isn't added with the element itself, but rather at it's very first mention:

  android:layout_above="@+id/Save" />
    <Button android:text="Save"
        android:id="@id/Save"
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号