【问题标题】:Scrollview and Tablelayout usage with banner to bottom in xmlScrollview 和 Tablelayout 在 xml 中使用横幅到底部
【发布时间】:2014-04-23 17:44:37
【问题描述】:

这是我的 xml 文件。我的应用中有 35 个这样的 xml 文件。他们的文本视图和按钮编号是不同的。其中一些超过 10 个,一些不到 5 个。但考虑到设备屏幕,我使用滚动视图设计了所有 xml 文件。我还需要tablelayout并使用了tablelayout,其中的表格行和tablerows内的textviews。当我完成应用程序时,一切正常。现在我在 xml 文件中添加了移动广告横幅,它开始崩溃。

如果我在表格布局结束之前添加横幅,应用程序不会崩溃,并且广告显示在表格的末尾,但视觉效果太差了。因为如果 xml 中有 3 行,那么横幅将在最后一个之后出现textview 这就像页面的中间。无论 xml 使用多少文本视图,我都希望它们是可滚动的,最后我希望横幅出现在页面的末尾。

我尝试了一些 android:gravity="bottom" 或

alignparentbottom="true" 的东西,但他们没有工作。在我的研究中,我意识到我可能需要添加一些线性或相对布局代码,但是当我尝试它们时,它们不适合滚动视图并崩溃。这是 xml 文件之一。我应该如何修改这个?

在图片中,第一个是它的工作原理,横幅出现在 textview 结束的地方,我想要第二张图片,无论横幅应该在页面底部多长时间。

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/blue">


    <TableLayout
        android:id="@+id/table"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <TableRow android:id="@+id/tableRow1" 
            android:layout_width="match_parent" 
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:padding="5dp"
                android:text="TRY"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#FFFFFF" >

            </TextView>
// some other buttons or textviews...


 </TableRow> 
 </TableLayout>
 <com.startapp.android.publish.banner.Banner 
  android:id="@+id/startAppBanner"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentBottom="true"
  android:layout_centerHorizontal="true"/>
</ScrollView>

【问题讨论】:

    标签: android layout scrollview gravity banner


    【解决方案1】:

    如果横幅需要始终显示在底部, 您可以将 LinearLayout 中的 ScrollView 的权重设置为 1。

    例子

    <?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"
    android:background="@drawable/blue">
        <ScrollView 
         android:layout_width="fill_parent"
         android:layout_height="0dip" 
         android:layout_weight="1"
         android:background="@drawable/blue">
    
    
    <TableLayout
        android:id="@+id/table"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <TableRow android:id="@+id/tableRow1" 
            android:layout_width="match_parent" 
            android:layout_height="wrap_content">
    
            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:padding="5dp"
                android:text="TRY"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#FFFFFF" >
    
            </TextView>
    // some other buttons or textviews...
        </TableRow> 
     </TableLayout>
    </ScrollView>
    <com.startapp.android.publish.banner.Banner 
     android:id="@+id/startAppBanner"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"/>
    </LinearLayout>
    

    【讨论】:

    • 没有这个没有解决问题。最初顶层是 scrollview ,在您的解决方案中,首先是线性布局,然后是滚动视图。我试过了,但在滚动视图中丢失了成员。一切都过去了。
    • Root LinearLayout 中的方向应该是垂直的。请查看更新后的xml
    • 看起来最接近的答案是这个。我尝试使用 textview 而不是横幅,它看起来不错,但是当我使用横幅时,广告没有显示。我不知道为什么它不显示广告。底部没有显示任何内容。
    【解决方案2】:

    我无法在此处测试代码,因为我没有 Banner 组件,但尝试将 TableLayout 和 Banner 包装在 LinearLayout 中。

    修改布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/blue">
        
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
    
            <TableLayout
                android:id="@+id/table"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >
                
                <TableRow android:id="@+id/tableRow1" 
                    android:layout_width="match_parent" 
                    android:layout_height="wrap_content">
                    
                    <TextView
                        android:id="@+id/textView1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@android:color/transparent"
                        android:padding="5dp"
                        android:text="TRY"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:textColor="#FFFFFF" />
        
                </TableRow> 
        
            </TableLayout>
            
            <com.startapp.android.publish.banner.Banner 
                android:id="@+id/startAppBanner"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true" />
        
        </LinearLayout>
            
    </ScrollView>
    

    更新

    在本地测试文件后,我意识到崩溃是由 Banner 引起的,因为 ScrollView 只能有一个孩子。我很惊讶您能够在布局文件中出现错误的情况下编译您的项目。因此,一种解决方案是将 TableLayout 和 Banner 包装在 LinearLayout 中。参见示例 1:

    示例 1

    <?xml version="1.0" encoding="utf-8"?>
    
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
    
      <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <TableLayout
          android:id="@+id/table"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
    
          <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <TextView
              android:id="@+id/textView1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:padding="5dp"
              android:text="TRY"
              android:textAppearance="?android:attr/textAppearanceMedium"
              />
    
          </TableRow>
    
        </TableLayout>
    
        <com.startapp.android.publish.banner.Banner 
            android:id="@+id/startAppBanner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true" />
    
      </LinearLayout>
    </ScrollView>
    

    想来想去,完全消除TableLayout或许是可以的。您可以使用 LinearLayout 将项目堆叠在一起。所以这会大大简化你的布局文件。请参见示例 2。

    示例 2

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/blue">
        
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >
    
            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:padding="5dp"
                android:text="TRY"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#FFFFFF" />
            
            <com.startapp.android.publish.banner.Banner 
                android:id="@+id/startAppBanner"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true" />
        
        </LinearLayout>
            
    </ScrollView>
    

    更新 2

    那么您的问题一定出在您的 Java 文件上。我已经在本地测试了布局文件,它确实可以正确渲染。请参阅下面的屏幕截图。

    更新 3

    解决方案需要更新布局文件和 Activity 类。逻辑相当简单。如果表格中的行数导致表格高度超过滚动视图的高度,则启用滚动。如果启用滚动,则横幅广告将作为表格的子项附加。如果禁用滚动,则横幅广告将作为线性布局的子项插入。为了确定正确的高度,必须订阅 OnGlobalLayout 事件。以下代码是您需要的**工作示例**。为了测试这两种情况(启用/禁用滚动),只需更改 ROW_COUNT 变量的值。我使用 3 禁用滚动,使用 30 启用滚动。您将看到横幅放置得当。

    activity_test.xml

    <?xml version="1.0" encoding="utf-8"?>
    
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/relativelayout"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
    
      <LinearLayout
        android:id="@+id/linearlayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <ScrollView
          android:id="@id/scrollview"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1"
          android:background="#e0e0e0">
    
          <TableLayout
            android:id="@+id/table"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
    
            <TableRow
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
    
              <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="20dp"
                android:text="TRY" />
    
            </TableRow>
    
    
          </TableLayout>
    
        </ScrollView>
    
      </LinearLayout>
    
    </RelativeLayout>
    

    TestActivity.java

    public class TestActivity extends Activity {
    
      private static String TAG = "TestActivity";
      private Activity mActivity;
      private LinearLayout mLinearLayout;
      private TableLayout mTableLayout;
      private RelativeLayout mRelativeLayout;
    
      private static int ROW_COUNT = 30;
    
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        Log.d(TAG, "onCreate");
    
        mActivity = this;
        mRelativeLayout = (RelativeLayout)findViewById(R.id.relativelayout);
        mLinearLayout = (LinearLayout)findViewById(R.id.linearlayout);
        mTableLayout = (TableLayout)findViewById(R.id.table);
    
        float density = getResources().getDisplayMetrics().density;
        final int padding = (int)Math.floor((density * 20f));
    
        for(int i=0;i<ROW_COUNT;i++){
          TextView view = new TextView(mActivity);
          view.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
          view.setPadding(padding,padding,padding,padding);
          view.setText("TableRow");
    
          TableRow row = new TableRow(mActivity);
          row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
          row.addView(view);
    
          mTableLayout.addView(row);
        }
    
        ViewTreeObserver observer = mRelativeLayout.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
          @Override
          public void onGlobalLayout() {
            TextView view = (TextView) findViewById(R.id.banner);
            if(view == null){
              view = new TextView(mActivity);
              view.setId(R.id.banner);
              view.setBackgroundColor(Color.LTGRAY);
              view.setText("BannerAd");
              view.setGravity(Gravity.CENTER_HORIZONTAL);
              view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
              view.setPadding(0, padding, 0, padding);
              if(mTableLayout.getHeight() > mLinearLayout.getHeight()) {
                mTableLayout.addView(view);
              }else{
                mLinearLayout.addView(view);
              }
            }
          }
        });
      }
    }
    

    如果需要,我可以发布屏幕截图,但请测试此代码,如果您有任何问题,请告诉我。

    【讨论】:

    • 谢谢你,但这没用,还是一样。横幅出现在最后一个文本视图结束的地方。其实你不需要尝试banner,你可以尝试一个按钮或者一个textview而不是banner,你可以测试它。它会做同样的事情。
    • 好的,我会用按钮试试,很快就会回复。
    • 谢谢你,我会等待的。
    • 请查看更新后的答案。你应该准备好了。
    • 没用,结果一样。我无法删除表格布局,我需要它,示例 1 没有解决问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多