2

我有一个数据输入类型的活动,我使用线性布局来均匀地分隔文本视图和编辑文本集。然后我有一个滚动视图,它应该可以让用户在软键盘启动时滚动。如果我使用android:fillViewport线性布局可以正常工作并填充屏幕并将每个项目均匀地展开,但是当键盘出现并停止每个项目均匀地展开时。如果我使用android:windowSoftInputMode="stateVisible|adjustPan",那么线性布局仍然展开,但滚动视图不再起作用(从所有未解决的帖子中,我认为您无法使用 adjustPan 获得有效的滚动视图)

有什么办法可以在滚动视图中使用线性布局,项目均匀分布并且在软键盘启动时仍然可以工作而不改变线性布局?

     <?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"
          android:fillViewport
           >


         <LinearLayout 
           android:gravity="left" 
           android:orientation="vertical" 
           android:id="@id/tab2" 
           android:paddingLeft="40.0dip"
           android:paddingTop="0.0dip" 
           android:paddingRight="40.0dip" 
           android:layout_width="fill_parent" 
           android:layout_height="wrap_content"
          >



        <LinearLayout 
              android:orientation="vertical"  
              android:layout_width="fill_parent" 
              android:layout_height="wrap_content" 
              android:layout_weight="1.0">
         <TextView 
              android:id="@id/textViewBrand" 
              android:layout_width="wrap_content" 
              android:layout_height="wrap_content" 
              android:text="@string/brand" />

        <AutoCompleteTextView
            android:id="@id/editTextBrand"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            >

        </AutoCompleteTextView>
        </LinearLayout>

  ...more linearlayouts with textview and edittext to be spaced out evenly
4

4 回答 4

1

在滚动视图中设置属性 android:fillViewport="true" 它将起作用

于 2014-10-02T10:15:37.220 回答
0

也许 ScrollView 不工作,因为你

android:layout_height 属性

定义为 match_parent。你必须把

包装内容

于 2013-08-06T11:06:58.240 回答
0
Follow This Code, I hope it resolves your Problem

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

    //Your Main Layout

    <LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:orientation="vertical"     
     android:weightSum="100">

    // First Sub Layout Under Main Layout
       <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" 
        android:layout_weight="10"
        android:weightSum="100" >

           <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="TextView" 
            android:layout_weight="70" />

           <EditText
            android:id="@+id/editText1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="30" />

        </LinearLayout>// Finishing First Sub layout 

// Second Sub Layout Under Main Layout
       <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" 
        android:layout_weight="10"
        android:weightSum="100" >

           <TextView
            android:id="@+id/textView2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="TextView" 
            android:layout_weight="70" />

           <EditText
            android:id="@+id/editText2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="30" />

        </LinearLayout>// Finishing Second Sub layout 

similarly for 3rd,4rth,5th sub layouts and so on........

</LinearLayout> // Finishing Main Layout
</ScrollView>   // Finishing ScrollView 
于 2013-08-06T11:52:15.947 回答
0

您可以查看 LinearLayout 的子视图,并在代码中使用ViewTreeObserver.OnGlobalLayoutListener. 以下代码将尺寸设置为不更改,因此您可以使用带android:fillViewport有权重的选项,然后基本上删除权重,但保留计算的尺寸。

我使用此代码进行了一些修改,以确保子视图均匀分布在 ScrollView 内的 LinearLayout 中,其中 LinearLayout 太大而android:fillViewport无法让权重实际工作。这是通过对孩子进行 2 次迭代来完成的。首先获取最大视图的高度,然后将所有子视图设置为该最大高度。

private void distributeViewsEvenlyInLinearLayout() {
    ViewTreeObserver observer = linearParentView.getViewTreeObserver();
    final ViewTreeObserver.OnGlobalLayoutListener globalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {

        @SuppressWarnings("deprecation")
        public void onGlobalLayout() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                linearParentView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            } else {
                linearParentView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }

            for (int i = 0; i < linearParentView.getChildCount(); i++) {
                View child = linearParentView.getChildAt(i);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, child.getHeight());
                child.setLayoutParams(layoutParams);
            }
        }
    };
    observer.addOnGlobalLayoutListener(globalLayoutListener);
}
于 2017-06-11T18:33:41.780 回答