2013-07-10 164 views
1

我想在scrollview中绘制居中的垂直线。我有两个galaxy 2s,一个是Android 4.2.2版本,另一个是2.3.7版本。垂直线不能画出

我的结果就只有在使用Android版本2.3.7(中间线)的观点是没有平局的Android版本4.2.2

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <View 
      android:layout_width="4dp" 
      android:layout_height="match_parent" 
      android:layout_gravity="center" 
      android:background="#000000" /> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" > 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Test text" /> 

     </RelativeLayout> 
    </FrameLayout> 

</ScrollView> 

example

下工作。

有谁知道最新的运行android 2.3.7的问题?

+0

使用视图的方法很好。我认为只有一个直接的孩子,FrameLayout可以更好地工作。为什么不使用LinearLayout而不是该框架布局? –

+0

首先,我使用了一个LinearLayout,但这不起作用。因为我可以集中线。当使用高度wrap_content在relavilayout中绘制线时,该线消失。 – suizo

+0

您是否尝试过使FrameLayout和scrollview fill_parent的高度? –

回答

0

我没有找到一个xml唯一的解决方案。

我的xml看起来像。请注意,该视图具有固定的高度,将在活动中被替换。

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <RelativeLayout 
     android:id="@+id/RelativeLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/ManualStartTextView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_centerHorizontal="true" 
      android:text="Start" /> 

     <RelativeLayout 
      android:id="@+id/CenterRelativLayout" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/ManualNoCardTableRow" > 

      <View 
       android:id="@+id/ManualTopGrayLine" 
       android:layout_width="1dp" 
       android:layout_height="200dp" 
       android:layout_centerHorizontal="true" 
       android:background="#000000" /> 

      <TextView 
       android:id="@+id/ManualStepTreeHeaderTextView" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/ManualStepTreeButton" 
       android:layout_marginLeft="3dp" 
       android:layout_marginTop="15dp" 
       android:layout_toRightOf="@+id/ManualTopGrayLine" 
       android:gravity="right" 
       android:text="3) Verifizieren" /> 

      <TextView 
       android:id="@+id/ManualStepTreeBodyTextView" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignLeft="@+id/ManualStepTreeHeaderTextView" 
       android:layout_below="@+id/ManualStepTreeHeaderTextView" 
       android:layout_marginLeft="3dp" 
       android:text="Lorem alfpha alsda a,si ljkasoi kjasbas asdop1 ,aspo9ij aslkdja9s oaisdjas0pd oasdn0p9m asd0p9" /> 
     </RelativeLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_below="@+id/CenterRelativLayout" 
      android:layout_marginLeft="15dp" 
      android:layout_marginRight="15dp" 
      android:orientation="vertical" > 

      <!-- some other content --> 

     </LinearLayout> 
    </RelativeLayout> 

</ScrollView> 

xml生成以下视图。

enter image description here

如果你想有一个动态的高度你行(图)您可以在下面的方法添加到您的活动。这会将线的高度设置为与RelativeLayout相同的高度。

@Override 
    public void onWindowFocusChanged(boolean hasFocus) { 
     super.onWindowFocusChanged(hasFocus); 
     if (hasFocus == true) { 
      View manualTopGrayLine = findViewById(R.id.ManualTopGrayLine); 
      RelativeLayout rlayout = (RelativeLayout) findViewById(R.id.CenterRelativLayout); 

      RelativeLayout.LayoutParams layoutParams = (android.widget.RelativeLayout.LayoutParams) manualTopGrayLine.getLayoutParams(); 
      layoutParams.height = rlayout.getHeight(); 
      manualTopGrayLine.setLayoutParams(layoutParams);    
     } 
    }