安卓扭曲的用户界面

10

我找不到以下失真的根本原因。
UI界面运行良好,但当我将屏幕旋转至横向时,滚动时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>

更新
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>


详细信息

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


更新 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();
    }
}
在Fragment中,我获取从Activity传递的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(Loader loader, Cursor cursor)中直接获取和填充我的动态UI视图。所有视图都是简单的文本视图,因此不使用任何适配器。

包含的布局是带有LinearLayout作为父容器和FrameLayout或LinearLayout作为子容器和其他视图的简单布局。

我正在使用Picasso库加载静态地图。 UPDATE 2 删除Picasso后问题仍然存在,因此Picasso不是问题所在。 设备:Nexus 5 Android操作系统:6.0.1

enter image description here


同时添加包含布局的XML。 - Vivek Mishra
你还需要为横屏模式设计一个布局。这样就可以了。 - Mustanser Iqbal
你可以在Activity标签的清单文件中添加以下代码:android:configChanges="keyboard|keyboardHidden|orientation|screenLayout"这样做可以避免在屏幕旋转时重新创建布局。 - Mustanser Iqbal
我已经尝试过了...但是没有解决我的问题!! - Abhinav Tyagi
3个回答

5
问题在于您的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);
    } 

1

请尝试按照示例更改XML。

思路是 - 您需要将整个内容放在相对布局中。一旦使用了相对布局,它就能让您将项目放置在彼此上方。

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

每个include应该有一个ID以供引用。

  1. 同时还需要指定宽度和高度,否则上方或下方的位置将被忽略。希望这可以帮到你,祝你生日聚会愉快:)提前祝福!

会去看一下。 谢谢祝福……还有5-6个月呢 ;) :D - Abhinav Tyagi
我明白了,你能否编辑你包含的布局文件以显示完整的细节?现在它被截断了,没有显示整个代码。 - Natarajan Raman
我们有一个 ScrollView -> LinearLayout -> LinearLayout -> RelativeLayout -> LinearLayout 的布局。您能否将 LinearLayout 设置为“v_details”的根视图,并将 RelativeLayout 放在其下面,然后尝试上述解决方案? - Natarajan Raman
抱歉,伙计,希望你能得到一些帮助并尽快解决问题。祝一切顺利! - Natarajan Raman
让我们在聊天中继续这个讨论 - Natarajan Raman

1
当屏幕方向改变时,请尝试调用decoreview的invalidate()方法。
 @Override
 public void onConfigurationChanged(Configuration newConfig) {
   super.onConfigurationChanged(newConfig);
     if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
         this.getWindow().getDecorView().invalidate();
    }
}

希望这能有所帮助。

这个想法是强制重新绘制布局... 这只是一个调查的步骤... 好的,尝试调用recreate()而不是this.getWindow().getDecorView().invalidate();... - M.Khouli
仍然是同样的问题 :( - Abhinav Tyagi
首先,我认为您需要在清单文件中的活动标记中添加“android:configChanges”属性。 <activity android:name="TestActivity" android:configChanges="orientation"> ...然后,您可以删除if语句...这样你的代码看起来像这样: @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); recreate(); } - M.Khouli
很奇怪 :( ... 你能否请粘贴你的Activity代码。 - M.Khouli
让我们在聊天中继续这个讨论 - Abhinav Tyagi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接