出于某种原因,我的框架布局不会一直延伸到屏幕边缘。
这是我的活动布局:
<FrameLayout
android:id="@+id/contactInfoFragmentContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
</FrameLayout>
以下是我在运行时放置在FrameLayout中的单独片段的布局:
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="220dp"
app:srcCompat="@android:drawable/btn_dialog"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="0dp" />
<LinearLayout
android:id="@+id/thumbnailsLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:orientation="horizontal">
<HorizontalScrollView
android:id="@+id/Thumbnails"
android:layout_width="260dp"
android:layout_height="97dp">
</HorizontalScrollView>
<Button
android:id="@+id/addImage"
android:layout_width="wrap_content"
android:layout_height="96dp"
android:layout_weight="1.06"
android:text="Add image" />
</LinearLayout>
<LinearLayout
android:id="@+id/shopNameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/nameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Name" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName" />
</LinearLayout>
<LinearLayout
android:id="@+id/phoneNumberLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Phone" />
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<EditText
android:id="@+id/phoneText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:id="@+id/addressLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText
android:id="@+id/addressText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPostalAddress" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"></LinearLayout>
</LinearLayout>
我只需在onCreate方法中添加片段:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_shop);
// Check that the activity is using the layout version with
// the fragment_container FrameLayout
if (findViewById(R.id.contactInfoFragmentContainer) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
// Create a new Fragment to be placed in the activity layout
ContactInfoFragment firstFragment = new ContactInfoFragment();
// In case this activity was started with special instructions from an
// Intent, pass the Intent's extras to the fragment as arguments
firstFragment.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.contactInfoFragmentContainer, firstFragment).commit();
}
}
那么问题是什么?当我在设计视图中查看片段布局时,它看起来很好!
问题似乎只有在将片段添加到我的活动布局后才会出现(参见上面的代码和布局)。我该如何解决这个问题?
编辑:我不明白为什么我收到了投票。至少你可以发表评论来解释这个问题的错误。答案 0 :(得分:1)
如何将FrameLayout
宽度设置为match_parent
!
编辑:
对于ConstraintLayout
,请将当前FrameLayout
替换为:
<FrameLayout
android:id="@+id/contactInfoFragmentContainer"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
并确保在xml中的根布局声明中有xmlns:app="http://schemas.android.com/apk/res-auto"
。