2016-03-21 119 views
9

我無法找到下面失真的根本原因。
UI工作正常,但是當我將屏幕旋轉到橫向時,UI在滾動時會變形。
當屏幕再次旋轉至肖像時,此失真繼續。Android扭曲的UI

<?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:layout_margin="8dp"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <include layout="@layout/i_details" /> 

     <include layout="@layout/v_details" /> 

     <include layout="@layout/d_details" /> 
    </LinearLayout> 


</ScrollView> 

UPDATE
i_details

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="4dp" 
    android:orientation="vertical"> 

    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="100dp" 
     android:background="@color/colorPrimary"> 

     <TextView 
      android:id="@+id/title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="left|bottom" 
      android:layout_margin="4dp" 
      android:text="Large Text" 
      android:textSize="25sp" /> 

    </FrameLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:elevation="4dp" 
     android:orientation="vertical"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="4dp" 
      android:layout_margin="8dp" 
      android:text="Message" 
      android:textSize="14sp" /> 

     <TextView 
      android:id="@+id/message" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="top|start" 
      android:layout_margin="8dp" 
      android:text="RANDOM TEXT" 
      android:textSize="18sp" /> 

    </LinearLayout> 

</LinearLayout> 


v_details

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_margin="4dp"> 

    <ImageView 
     android:id="@+id/map" 
     android:layout_width="match_parent" 
     android:layout_height="100dp" 
     android:adjustViewBounds="true" 
     android:scaleType="centerCrop" 
     android:layout_alignParentTop="true" 
     android:background="@color/colorPrimary" /> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/navigateFab" 
     android:layout_width="61dp" 
     android:layout_height="61dp" 
     android:layout_gravity="bottom|end" 
     android:layout_marginRight="12dp" 
     android:layout_marginBottom="-24dp" 
     android:background="@color/colorAccent" 
     android:contentDescription="navigation" 
     android:elevation="8dp" 
     android:src="@drawable/ic_navigate_24dp" 
     app:pressedTranslationZ="2dp" 
     android:layout_above="@+id/linearLayout" 
     android:layout_alignParentEnd="true" /> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:elevation="4dp" 
     android:layout_below="@id/map" 
     android:orientation="vertical" 
     android:id="@+id/linearLayout"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="8dp" 
      android:layout_marginTop="4dp" 
      android:text="Random Text" 
      android:textSize="14sp" /> 

     <TextView 
      android:id="@+id/name" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="8dp" 
      android:gravity="top|start" 
      android:text="Random Text" 
      android:textSize="18sp" /> 

     <TextView 
      android:id="@+id/address" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="8dp" 
      android:drawablePadding="4dp" 
      android:drawableStart="@drawable/ic_place_24dp" 
      android:gravity="top|start" 
      android:text="Random Text" 
      android:textSize="18sp" /> 

     <TextView 
      android:id="@+id/phone" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="8dp" 
      android:autoLink="phone" 
      android:clickable="true" 
      android:drawablePadding="4dp" 
      android:drawableStart="@drawable/ic_call_24dp" 
      android:gravity="top|start" 
      android:text="Random Text" 
      android:textSize="16sp" /> 

     <TextView 
      android:id="@+id/website" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="8dp" 
      android:autoLink="web" 
      android:clickable="true" 
      android:drawablePadding="4dp" 
      android:drawableStart="@drawable/ic_language_24dp" 
      android:gravity="top|start" 
      android:text="Random Text" 
      android:textSize="16sp" /> 

    </LinearLayout> 

</RelativeLayout> 


d_details

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="4dp" 
    android:elevation="4dp" 
    android:orientation="vertical"> 


    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="8dp" 
     android:layout_marginTop="4dp" 
     android:text="Random Text" 
     android:textSize="14sp" /> 

    <TextView 
     android:id="@+id/name" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="8dp" 
     android:gravity="top|start" 
     android:text="Random Text" 
     android:textSize="18sp" /> 

    <TextView 
     android:id="@+id/address" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="8dp" 
     android:drawablePadding="4dp" 
     android:drawableStart="@drawable/ic_place_24dp" 
     android:gravity="top|start" 
     android:text="Random Text" 
     android:textSize="18sp" /> 

    <TextView 
     android:id="@+id/phone" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="8dp" 
     android:autoLink="phone" 
     android:clickable="true" 
     android:drawablePadding="4dp" 
     android:drawableStart="@drawable/ic_call_24dp" 
     android:gravity="top|start" 
     android:text="Random Text" 
     android:textSize="16sp" /> 

    <TextView 
     android:id="@+id/email" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="8dp" 
     android:autoLink="email" 
     android:clickable="true" 
     android:drawablePadding="4dp" 
     android:drawableStart="@drawable/ic_mail_24dp" 
     android:gravity="top|start" 
     android:text="Random Text" 
     android:textSize="16sp" /> 


</LinearLayout> 


UPDATE 3
附加活動代碼並表示片段邏輯


i_activity

public class MYActivity extends AppCompatActivity { 
    private static final String LOG_TAG = MYActivity.class.getSimpleName(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_invitation); 

     MYFragment myFragment = new MYFragment(); 

     // In case this activity was started with special instructions from an 
     // Intent, pass the Intent's extras to the fragment as arguments 
     myFragment.setArguments(getIntent().getExtras()); 

     // Add the fragment to the 'my_container' FrameLayout 
     getSupportFragmentManager().beginTransaction().add(R.id.my_container, myFragment).commit(); 
    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     recreate(); 
    } 
} 


在片段,我取從活性傳遞的ID,填充我的靜態UI,
發起CURSOR LOADER

@Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     Log.d(LOG_TAG, "Activity Created"); 
     getLoaderManager().initLoader(CURSOR_LOADER, getArguments(), this); 
     super.onActivityCreated(savedInstanceState); 
    } 


onLoadFinished(裝載機裝載機,光標光標)
獲取並直接填充我的動態UI的看法。
由於所有視圖均爲簡單文本視圖,因此不使用任何適配器。

包含的佈局是簡單的佈局,LinearLayout作爲父容器,FrameLayout或LinearLayout作爲子視圖和其他視圖。

我正在使用Picasso lib加載靜態地圖。
更新2 畢加索不是問題,因爲刪除畢加索後,問題依然存在。
設備:Nexus 5的 Android操作系統:6.0.1

enter image description here

+0

添加xml包含的佈局也 –

+0

您還需要設計橫向模式的佈局。那麼它會很好。 –

+0

你也可以在活動標記中加入這條線: android:configChanges =「keyboard | keyboardHidden | orientation | screenLayout」通過添加這個,當你旋轉屏幕時你的佈局不會再被創建 –

回答

4

問題是您的Fragment創建了兩次方向更改 在創建新的Fragment對象之前檢查saveInstancestate。

private MYFragment myfragment;//define fragment globally 

    if(savedInstanceState == null) { 
     myfragment= new MYFragment();//set tag for fragment 
     getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, headlines, FRAGMENT_TAG_STRING).commit(); // Use tags, it's simpler to deal with 

    } else { 
     myfragment= getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG_STRING); 
    } 
+0

非常感謝:D –

1

請試着改變你的XML作爲樣本如下圖所示。

想法是 - 你需要保持整個東西在相對佈局。一旦你使用Relative佈局,它可以讓你把項目放在另一個之上。

<?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:layout_margin="8dp"> 

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

      <include layout="@layout/activity_line_graph" 
       android:id="@+id/two" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/one"/> 

      <include layout="@layout/activity_my_stocks" 
       android:id="@+id/three" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/two"/> 

      <include layout="@layout/widget_collection" 
       android:id="@+id/four" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/three"/> 
     </LinearLayout> 
     </RelativeLayout> 

    </ScrollView> 

每個包括應,

  1. 的ID將被提及。
  2. 也有寬度和高度,否則,忽略上面或下面的位置。 希望這有助於,嘿,我希望你有一個美好的生日派對:)提前祝福!
+0

會檢查出來。 感謝您的願望...仍然5-6個月;):D –

+0

沒有幫助:( –

+0

我明白了,請您編輯您的附帶佈局以顯示完整的詳細信息?截至目前,它已被切斷,不 –

1

嘗試在方向更改時調用decoreview的invalidate()方法。

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 
     this.getWindow().getDecorView().invalidate(); 
    } 
} 

希望這會有所幫助。

+0

未解決:( –

+0

這個想法是強制佈局重繪...這只是一個步驟來調查...確定嘗試調用recreate();而不是this.getWindow()。getDecorView()。invalidate(); ... –

+0

仍然是相同的問題:( –