【问题标题】:Android - Set gravity of elements in XMLAndroid - 在 XML 中设置元素的重力
【发布时间】:2013-04-02 10:02:33
【问题描述】:

这是 XML 代码:

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
    android:layout_height="wrap_content">

<RelativeLayout
        android:id="@+id/relative_layout_main"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

    <com.adsdk.sdk.banner.AdView
        android:id="@+id/ad_1"
        android:layout_width="300dp"
        android:background="#000000"
        android:layout_height="50dp"
        request_url="http://my.mobfox.com/request.php"
        publisherId="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        location="true"
        animation="true" />

    <TextView
        android:textIsSelectable="true"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:textStyle="bold"
        android:id="@+id/error_view_2"
        android:layout_marginTop="20dp"
    android:layout_below="@id/ad_1"
        android:text="@string/gen_mess_err" />    

    <TextView
        android:textIsSelectable="true"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:layout_below="@id/error_view_2"
        android:id="@+id/error_view_1" />

    <com.adsdk.sdk.banner.AdView
        android:id="@+id/ad_2"
        android:layout_width="300dp"
        android:background="#000000"
        android:layout_height="50dp"
        request_url="http://my.mobfox.com/request.php"
        publisherId="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:layout_below="@id/error_view_1"
        android:gravity="bottom"
        location="true"
        animation="true" />

</RelativeLayout>

我想要的是将广告设置在屏幕的顶部和底部,并将 textview 设置在屏幕的中心,但这不起作用。我该怎么办?

提前谢谢 :)

【问题讨论】:

    标签: android layout center gravity


    【解决方案1】:

    将您的 XML 代码更改为此

    android:fillViewport must be set to true - 当设置为 true 时,如果需要,此属性会使滚动视图的子视图扩展到 ScrollView 的高度。当 child 高于 ScrollView 时,该属性不起作用。

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >
    <RelativeLayout
        android:id="@+id/relative_layout_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <com.adsdk.sdk.banner.AdView
            android:id="@+id/ad_1"
            android:layout_width="300dp"
            android:layout_height="50dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            animation="true"
            location="true"
            publisherId="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
            request_url="http://my.mobfox.com/request.php"
            android:background="#000000"
            android:gravity="center" />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:orientation="vertical" >
            <TextView
                android:id="@+id/error_view_2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:gravity="center"
                android:text="@string/gen_mess_err"
                android:textIsSelectable="true"
                android:textStyle="bold" />
            <TextView
                android:id="@+id/error_view_1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textIsSelectable="true" />
        </LinearLayout>
        <com.adsdk.sdk.banner.AdView
            android:id="@+id/ad_2"
            android:layout_width="300dp"
            android:layout_height="50dp"
            android:layout_alignParentBottom="true"
            android:layout_below="@id/error_view_1"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dp"
            animation="true"
            location="true"
            publisherId="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
            request_url="http://my.mobfox.com/request.php"
            android:background="#000000"
            android:gravity="bottom" />
    </RelativeLayout>
    

    【讨论】:

    • 谢谢,它可以工作:) 但是我尝试在我的代码中使用 android:layout_alignParentBottom="true" 并修改它的一些参数,无论如何谢谢:)
    【解决方案2】:

    为什么ScrollViewlayout_height 属性设置为wrap_content?应该是match_parent 才能填满屏幕,这样广告就被放置在顶部和底部。

    此外,fill_parent 属性已过时,match_parent 是其较新的值 :)

    【讨论】:

      【解决方案3】:

      你这个

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical"
          >
          <com.adsdk.sdk.banner.AdView
                  android:id="@+id/ad_1"
                  android:layout_width="fill_parent"
                  android:background="#000000"
                  android:layout_height="wrap_content"
                  request_url="http://my.mobfox.com/request.php"
                  publisherId="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
                  android:layout_centerHorizontal="true"
                  android:gravity="center"
                  location="true"
                  animation="true"  />
      
          <ScrollView
                  android:id="@+id/relative_layout_main"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:orientation="vertical"
                  >
      
          <TextView
                  android:textIsSelectable="true"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:gravity="center"
                  android:textStyle="bold"
                  android:id="@+id/error_view_2"
                  android:layout_marginTop="20dp"
                  android:text="string/gen_mess_err" />
      
          <TextView
                  android:textIsSelectable="true"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:gravity="center"
                  android:id="@+id/error_view_1" />
          </ScrollView>
      
          <com.adsdk.sdk.banner.AdView
                  android:id="@+id/ad_2"
                  android:layout_width="fill_parent"
                  android:background="#000000"
                  android:layout_height="wrap_content"
                  request_url="http://my.mobfox.com/request.php"
                  publisherId="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
                  android:layout_marginTop="-51dp"
                  android:gravity="bottom"
                  location="true"
                  animation="true"
                  />
      

      希望有帮助。

      【讨论】:

        猜你喜欢
        • 2012-07-01
        • 1970-01-01
        • 2021-02-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-27
        • 1970-01-01
        相关资源
        最近更新 更多