【发布时间】:2016-03-13 00:33:31
【问题描述】:
我在尝试使用 replace 方法替换片段以使用另一个地图片段登录服务器时遇到了问题,但这就是发生的情况:新片段(应该是亚马逊地图片段)only takes half of the screen。一旦用户点击登录按钮,它应该变成新的片段(我也尝试过改变片段,但我也无法让它工作)。
目前,我正在从异步任务的 onPostExecute 方法调用新片段,该方法检查登录是否正常工作。这是我称之为交易的地方:
@Override
protected void onPostExecute(Boolean result) {
if (result) { //If true, the server tasks were successful
getFragmentManager()
.beginTransaction()
.replace(R.id.fragment_login_view, createFragment())
.commit();
}
}
这是我调用的第二个片段的 XML 文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent">
<fragment xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/fragment_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:amzn_mapType="amzn_normal"
tools:ignore="MissingPrefix"
class="com.amazon.geo.mapsv2.MapFragment" />
</RelativeLayout>
我已经尝试了很多方法来更改此 XML 文件,例如列出的所有内容 here 和 here,以及亚马逊提供的文档中列出的几乎所有内容,以及各种在线教程。
我也查看了a question for replacing fragments in an Async task,但无济于事。我知道替换有点工作,从我在上面发布的图片中可以看出,但它似乎从未占据整个屏幕。
我的地图片段类的onCreate和onCreateView如下:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (isAmazonMapsAvailable()){ //this has always been true
Log.i(TAG, "Amazon maps is available!");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_map, container, false);
return v;
}
我花了几个小时来处理这段代码,但无法让地图片段填满整个屏幕——同时仍然显示地图。我认为它一定与我的 XML 文件有关,因为我已经能够让它显示片段的一部分,只是不是所有的片段都是正确的。我认为我正确地进行了替换,并且由于我的 Async 任务是嵌套的,因此替换能够在那里正常工作。
任何帮助或指导将不胜感激!非常感谢!
编辑
这是 login_fragment XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_login_view"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:id="@+id/userNameHint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/user_name"
android:paddingTop="25dp"
android:paddingBottom="25dp"
android:paddingStart="24dp"
android:paddingEnd="24dp"/>
<TextView
android:id="@+id/passwordHint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/password"
android:paddingTop="24dp"
android:paddingBottom="25dp"
android:paddingStart="24dp"
android:paddingEnd="24dp"/>
<TextView
android:id="@+id/serverHostHint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/server_host"
android:paddingTop="24dp"
android:paddingBottom="25dp"
android:paddingStart="24dp"
android:paddingEnd="24dp"/>
<TextView
android:id="@+id/serverPortHint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/server_port"
android:paddingTop="24dp"
android:paddingBottom="25dp"
android:paddingStart="24dp"
android:paddingEnd="24dp"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="@+id/userNameEntry"
android:layout_width="150dp"
android:layout_height="match_parent"
android:inputType="text"
android:paddingBottom="22dp"/>
<EditText
android:id="@+id/passwordEntry"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:paddingTop="22dp"
android:paddingBottom="22dp"
android:allowUndo="true"/>
<EditText
android:id="@+id/serverHostEntry"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:inputType="text"
android:paddingTop="22dp"
android:paddingBottom="22dp"/>
<EditText
android:id="@+id/serverPortEntry"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:inputType="text"
android:paddingTop="22dp"
android:paddingBottom="22dp"/>
</LinearLayout>
<Button
android:id="@+id/signInButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sign_in"
android:layout_gravity="center_vertical" />
</LinearLayout>
【问题讨论】:
-
R.id.fragment_login_view所在的 XML 是什么样的?同样在Map Fragment的 XML 中,RelativeLayout很可能是多余的,可以删除。 -
@Darwind 我已经在 XML 文件中添加了登录片段。不过,您对 RelativeLayout 是正确的。我不知道为什么我把它放在那里。我从我的代码中删除它并重新运行它,但我仍然遇到同样的问题。
标签: java android xml android-fragments