gpt4 book ai didi

java - 在方向更改中更改 fragment 宽度

转载 作者:行者123 更新时间:2023-12-02 05:13:44 25 4
gpt4 key购买 nike

我目前正在学习 Android Studio 开发,并且被教程的练习绊倒了(卡住了近 2 周)。它需要我使用 Fragment 来创建这种外观:

See Image

这是我的代码:

home.java

public class home extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);

Intent previousIntent = getIntent();
String username = previousIntent.getStringExtra("username");

Bundle sendDataToBody = new Bundle();
sendDataToBody.putString("username", username);

// Fragment

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

WindowManager windowManager = getWindowManager();
Point size = new Point();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
windowManager.getDefaultDisplay().getSize(size);

if (size.x > size.y) {
// landscape

Menu fragmentMenu = new Menu();
body fragmentBody = new body();

fragmentBody.setArguments(sendDataToBody);

fragmentTransaction.add(R.id.menuFrame, fragmentMenu);
fragmentTransaction.add(R.id.bodyFrame, fragmentBody);
} else {
// portrait

Menu fragmentMenu = new Menu();
fragmentTransaction.add(R.id.menuFrame, fragmentMenu);
}
} else {
Display display = windowManager.getDefaultDisplay();

if (display.getRotation() == Surface.ROTATION_90
|| display.getRotation() == Surface.ROTATION_270) {
// landscape


Menu fragmentMenu = new Menu();
body fragmentBody = new body();

fragmentBody.setArguments(sendDataToBody);

fragmentTransaction.add(R.id.menuFrame, fragmentMenu);
fragmentTransaction.add(R.id.bodyFrame, fragmentBody);
} else {
//portrait
Menu fragmentMenu = new Menu();
fragmentTransaction.add(R.id.menuFrame, fragmentMenu);
}
}

fragmentTransaction.commit();
// End of Fragment
}
}

和我的 home.xml :

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="@dimen/container_padding_vertical"
android:paddingBottom="@dimen/container_padding_vertical"
android:paddingRight="@dimen/container_padding_horizontal"
android:paddingLeft="@dimen/container_padding_horizontal"
android:orientation="vertical"
tools:context=".home">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/menuFrame"></FrameLayout>


<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/bodyFrame"></FrameLayout>

</LinearLayout>

正如你所看到的,如果我运行这些代码,在纵向模式下它会像图片一样工作。

但是当我将方向更改为横向时,该按钮仍然全屏显示。

如何将按钮更改为仅像图片这样的部分?

注意:这是我在 stackOverflow 中关于 android 的第一个问题。我希望我的解释足够清楚

编辑

这是我的menuFragment.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".Menu">

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/convertBtn"
android:text="Convert USD/IDR" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/logoutBtn"
android:text="Logout"/>

</LinearLayout>

这是我的 bodyFragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".body">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/username_show"/>

</LinearLayout>

最佳答案

您在这里想要做的是使用资源框架为每个 fragment 和 Activity 提供两种不同的布局。一种布局适用于纵向,另一种布局适用于横向。

结构如下:

res/
layout/
activity_home.xml
fragment_body.xml
fragment_menu.xml
layout-land/
activity_home.xml
fragment_body.xml
fragment_menu.xml

您无需在代码中执行任何操作。只需在您的 Activity 中使用 setContentView(R.layout.activity_home) ,系统就会自动为您选择正确的版本(纵向或横向)。

同样,在您的 Fragment 中,您可以编写 inflater.inflate(R.layout.fragment_body,parent, false),系统会自动为您选择正确的版本。

现在,您只需确保不同的 xml 文件执行您想要的操作即可。例如,在纵向模式中,您可以使按钮与屏幕宽度相匹配:

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
.../>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
.../>

但是在你的风景中你可以让你的按钮换行:

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
.../>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
.../>

只要您在横向和纵向版本中使用相同的id,一切都会“正常工作”。

关于java - 在方向更改中更改 fragment 宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56311783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com