安卓扭曲的用户界面

标签 android android-layout user-interface screen-orientation

我无法找到以下失真的根本原因。
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>


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>


更新 3
添加 Activity 代码并声明 fragment 逻辑


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,
启动光标加载器

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


onLoadFinished(Loader 加载器, Cursor 游标)
直接获取并填充我的动态 UI View 。
不使用任何适配器,因为所有 View 都是简单的 TextView 。

包含的布局是简单的布局,其中 LinearLayout 作为父容器,FrameLayout 或 LinearLayout 作为 subview 和其他 View 。

我正在使用 Picasso 库加载静态 map 。
更新 2 Picasso 不是问题,因为在删除 Picasso 之后,问题仍然存在。
设备:Nexus 5 安卓操作系统:6.0.1

enter image description here

最佳答案

问题是您的 fragment 在方向更改时创建了两次 在创建新的 fragment 对象之前检查 savedInstancestate。

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);
    } 

关于安卓扭曲的用户界面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36123579/

相关文章:

java - ArrayList的值在匿名内部类之外是不同的

android - 在 Android 中创建自定义布局

android - 水平居中对齐布局中的两个 TextView

web - 为什么登录字段大多是右对齐的?

ios - 关闭键盘后让 inputAccessoryView 可见

android - 获取存储在手机中的android用户名

android - 无法删除标题栏

java - Android Studio 中缺少可绘制文件夹

android - 输入耗时(一些对话?)

java - Java 中的 repaint() GUI 问题