Textview Gravity在scrollview中无法正常工作

时间:2014-09-12 11:58:30

标签: android

 <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/layouts"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >
      <ScrollView
    android:id="@+id/SCROLLER_ID"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
     >

    <LinearLayout
        android:id="@+id/images"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
           android:lineSpacingMultiplier="1.2"
            android:padding="12dp"

            />

        <TextView
            android:id="@+id/footer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center"
            android:layout_weight="1"
            android:gravity="bottom|center" />
    </LinearLayout>
    </ScrollView>
   </RelativeLayout>

我正试图在视图中为textview设置Gravity底部。但是我惊讶于Gravity没有工作。我可以通过编写params.setMargins(20,700,20,8)来做到这一点。但对于所有类型的设备而言,这不是一个好的选择。

   This is my XML Layout:
  <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/layouts"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="horizontal" >


   <LinearLayout
    android:id="@+id/images"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ScrollView
        android:id="@+id/SCROLLER_ID"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         >

        <TextView
            android:id="@+id/text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
             />

        </ScrollView>

    <TextView
        android:id="@+id/footer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="bottom" />
        </LinearLayout>
    </RelativeLayout>

   This is My ViewPagerAdapter class:

     public class ViewPagerAdapter extends PagerAdapter
        {


     public Object instantiateItem(ViewGroup container, int position) 
     {
     LinearLayout layouts;
     TextView countpages;
     inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     View itemview=inflater.inflate(R.layout.main_layout,container,false);

      layouts=(LinearLayout) itemview.findViewById(R.id.images);
      textview=(TextView) itemview.findViewById(R.id.text);
      countpages=(TextView) itemview.findViewById(R.id.footer);

         LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
         (new  LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
          params.gravity=Gravity.BOTTOM;    // This is not working
          textview.setLayoutParams(params);

      } 

2 个答案:

答案 0 :(得分:0)

不需要ScrollView。您可以直接使TextView中的文本可滚动。 供参考检查this link

答案 1 :(得分:0)

无需RelativeLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1"
        android:background="#FFFF0000">
        <!-- scrollable contentn -->
    </ScrollView>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="some text"/>
</LinearLayout>

修改 还有一件事 - 始终在layout文件中而不是在类中设置边距。