我在 android 页面中有这种情况:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="@drawable/border">
<RelativeLayout
android:id="@+id/lbltvFirstContainer"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="90" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/lbltvFirstText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:visibility="visible"
android:layout_gravity="left"
android:textSize="19dp"
android:textStyle="bold" />
<TextView
android:id="@+id/tvSecondText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:visibility="visible"
android:layout_toRightOf="@+id/lbltvFirstText"
android:textColor="@color/green"
android:textSize="19dp"
android:textStyle="bold" />
<com.google.android.flexbox.FlexboxLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:flexDirection="row"
app:justifyContent="flex_end"
app:flexWrap="wrap"
app:layout_flexBasisPercent="50%"
app:alignItems="flex_start"
app:alignContent="flex_start"
app:showDividerVertical="middle"
app:dividerDrawable="@drawable/divider_flexbox">
<com.google.android.material.button.MaterialButton
android:id="@+id/btnButton"
android:background="@android:color/transparent"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_marginTop="-5dp"
app:icon="@drawable/btnButton"
app:iconTint="@null" />
<ImageView
android:id="@+id/first_icon"
android:src="@drawable/grey_24dp"
android:background="@android:color/transparent"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_weight="0.5"
android:layout_marginRight="4dp"
android:layout_marginTop="4dp"
android:layout_alignParentRight="true"/>
</com.google.android.flexbox.FlexboxLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/elements_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/lblThirdImage"
android:layout_width="32dp"
android:layout_height="32dp"
android:src="@drawable/icon_image"
android:layout_marginLeft="4dp"
android:layout_gravity="start" />
<TextView
android:id="@+id/tvThirdText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:padding="4dp"
android:layout_toRightOf="@+id/lblThirdImage"
android:gravity="start"
android:textSize="18dp" />
</RelativeLayout>
<com.google.android.flexbox.FlexboxLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:flexWrap="wrap"
app:alignItems="stretch"
app:alignContent="stretch"
app:showDividerVertical="middle"
app:dividerDrawable="@drawable/divider" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/lblFourImage"
android:layout_width="32dp"
android:layout_height="32dp"
android:src="@drawable/four_image"
android:visibility="visible"
android:layout_marginLeft="4dp"
android:layout_gravity="start" />
<TextView
android:id="@+id/tvFourText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:padding="4dp"
android:visibility="visible"
android:layout_toRightOf="@+id/lbltvFourImage"
android:gravity="start"
android:textSize="18dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/lbltvFiveImage"
android:layout_width="32dp"
android:layout_height="32dp"
android:visibility="visible"
android:layout_marginLeft="4dp"
android:layout_gravity="start"
android:src="@drawable/five_image" />
<TextView
android:id="@+id/tvFiveText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:padding="4dp"
android:visibility="visible"
android:layout_toRightOf="@+id/lbltvFiveImage"
android:gravity="start"
android:textSize="18dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/lbltvSixImage"
android:layout_width="32dp"
android:layout_height="32dp"
android:visibility="visible"
android:layout_marginLeft="4dp"
android:layout_gravity="start"
android:src="@drawable/six_image" />
<TextView
android:id="@+id/tvSixText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:padding="4dp"
android:visibility="visible"
android:layout_toRightOf="@+id/lbltvSixImage"
android:gravity="start"
android:textSize="18dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/lbltvSevenImage"
android:layout_width="32dp"
android:layout_height="32dp"
android:src="@drawable/seven_image"
android:visibility="visible"
android:layout_marginLeft="4dp"
android:layout_gravity="start" />
<TextView
android:id="@+id/tvSevenImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:padding="4dp"
android:visibility="visible"
android:layout_toRightOf="@+id/lbltvSevenImage"
android:gravity="start"
android:textSize="18dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/tvEightImage"
android:src="@drawable/eigth_image"
android:background="@android:color/transparent"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginRight="4dp"
android:layout_marginTop="10dp"
android:layout_alignParentRight="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/tvNineImage"
android:src="@drawable/nine_image"
android:background="@android:color/transparent"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginTop="10dp"
android:layout_alignParentRight="true"/>
<com.google.android.material.button.MaterialButton
android:id="@+id/lblTenImage"
android:background="@android:color/transparent"
android:layout_width="42dp"
android:layout_height="42dp"
app:icon="@drawable/ten_image"
app:iconTint="@null" />
</LinearLayout>
</com.google.android.flexbox.FlexboxLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/lbltvSecondContainer"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="10">
</RelativeLayout>
</LinearLayout>
而且我正在尝试并排放置两个容器,lbltvFirstContainer
并且lbltvSecondContainer
,我将在其中按百分比设置宽度,因此在第二个容器中我可以只放一个“>”,就像一个按钮一样转到另一个页面,所以在搜索后我找到了可以使用的地方那些app:layout_constraintWidth_default="percent"
和app:layout_constraintWidth_percent="10"
但看起来不起作用....你有一个方向来制作这两个具有百分比宽度的主要容器吗?提前致谢!
干杯
编辑:
在@Cheticamp 的建议之后,我更改为:
<?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="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="@drawable/border">
<androidx.constraintlayout.widget.ConstraintLayout
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">
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/left_guideline"
app:layout_constraintGuide_percent=".80"
android:orientation="horizontal" />
<RelativeLayout
android:id="@+id/left_container"
android:layout_width="0dp"
android:layout_height="match_parent"
app:layout_constraintEnd_toStartOf="@+id/left_guideline"
app:layout_constraintStart_toStartOf="parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/lbltvFirstText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:visibility="visible"
android:layout_gravity="left"
android:textSize="19dp"
android:textStyle="bold"
android:text="Matricola:" />
<TextView
android:id="@+id/tvSecondText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:visibility="visible"
android:layout_toRightOf="@+id/lbltvFirstText"
android:textColor="@color/area_frg"
android:textSize="19dp"
android:textStyle="bold" />
<com.google.android.flexbox.FlexboxLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:flexDirection="row"
app:justifyContent="flex_end"
app:flexWrap="wrap"
app:layout_flexBasisPercent="50%"
app:alignItems="flex_start"
app:alignContent="flex_start"
app:showDividerVertical="middle"
app:dividerDrawable="@drawable/divider_flexbox">
<com.google.android.material.button.MaterialButton
android:id="@+id/btnButton"
android:background="@android:color/transparent"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_marginTop="-5dp"
app:icon="@drawable/btnButton"
app:iconTint="@null" />
<ImageView
android:id="@+id/first_icon"
android:src="@drawable/grey_24dp"
android:background="@android:color/transparent"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_weight="0.5"
android:layout_marginRight="4dp"
android:layout_marginTop="4dp"
android:layout_alignParentRight="true"/>
</com.google.android.flexbox.FlexboxLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/lblThirdImage"
android:layout_width="32dp"
android:layout_height="32dp"
android:src="@drawable/wifi"
android:layout_marginLeft="4dp"
android:layout_gravity="start" />
<TextView
android:id="@+id/tvThirdText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:padding="4dp"
android:layout_toRightOf="@+id/lblThirdImage"
android:gravity="start"
android:textSize="18dp" />
</RelativeLayout>
<com.google.android.flexbox.FlexboxLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:flexWrap="wrap"
app:alignItems="stretch"
app:alignContent="stretch"
app:showDividerVertical="middle"
app:dividerDrawable="@drawable/divider" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/lblFourImage"
android:layout_width="32dp"
android:layout_height="32dp"
android:src="@drawable/thermo"
android:visibility="visible"
android:layout_marginLeft="4dp"
android:layout_gravity="start" />
<TextView
android:id="@+id/tvFourText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:padding="4dp"
android:visibility="visible"
android:layout_toRightOf="@+id/lblFourImage"
android:gravity="start"
android:textSize="18dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/lbltvFiveImage"
android:layout_width="32dp"
android:layout_height="32dp"
android:visibility="visible"
android:layout_marginLeft="4dp"
android:layout_gravity="start"
android:src="@drawable/full_batt" />
<TextView
android:id="@+id/tvFiveText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:padding="4dp"
android:visibility="visible"
android:layout_toRightOf="@+id/lbltvFiveImage"
android:gravity="start"
android:textSize="18dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/lbltvSixImage"
android:layout_width="32dp"
android:layout_height="32dp"
android:visibility="visible"
android:layout_marginLeft="4dp"
android:layout_gravity="start"
android:src="@drawable/power" />
<TextView
android:id="@+id/tvSixText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:padding="4dp"
android:visibility="visible"
android:layout_toRightOf="@+id/lbltvSixImage"
android:gravity="start"
android:textSize="18dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/lbltvSevenImage"
android:layout_width="32dp"
android:layout_height="32dp"
android:src="@drawable/gsm"
android:visibility="visible"
android:layout_marginLeft="4dp"
android:layout_gravity="start" />
<TextView
android:id="@+id/tvSevenText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:padding="4dp"
android:visibility="visible"
android:layout_toRightOf="@+id/lbltvSevenImage"
android:gravity="start"
android:textSize="18dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/tvEightImage"
android:src="@drawable/eight_image"
android:background="@android:color/transparent"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginRight="4dp"
android:layout_marginTop="10dp"
android:layout_alignParentRight="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/tvNineImage"
android:src="@drawable/nine_image"
android:background="@android:color/transparent"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginTop="10dp"
android:layout_alignParentRight="true"/>
<com.google.android.material.button.MaterialButton
android:id="@+id/lblTenImage"
android:background="@android:color/transparent"
android:layout_width="42dp"
android:layout_height="42dp"
app:icon="@drawable/play"
app:iconTint="@null" />
</LinearLayout>
</com.google.android.flexbox.FlexboxLayout>
</RelativeLayout>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/right_guideline"
app:layout_constraintGuide_percent=".20"
android:orientation="horizontal" />
<RelativeLayout
android:id="@+id/right_container"
android:layout_width="0dp"
android:layout_height="match_parent"
app:layout_constraintEnd_toStartOf="@+id/right_guideline"
app:layout_constraintStart_toStartOf="parent">
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
使用这些ConstraintLayout
,但似乎它一直返回此错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mytestapp, PID: 32175
java.lang.AssertionError: LEFT
at androidx.constraintlayout.solver.widgets.Guideline.getAnchor(Guideline.java:147)
at androidx.constraintlayout.solver.widgets.ConstraintWidget.immediateConnect(ConstraintWidget.java:1620)
at androidx.constraintlayout.widget.ConstraintLayout.applyConstraintsFromLayoutParams(ConstraintLayout.java:1359)
at androidx.constraintlayout.widget.ConstraintLayout.setChildrenConstraints(ConstraintLayout.java:1253)
at androidx.constraintlayout.widget.ConstraintLayout.updateHierarchy(ConstraintLayout.java:1149)
at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1685)
at android.view.View.measure(View.java:25532)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
at android.view.View.measure(View.java:25532)
at android.widget.ListView.measureScrapChild(ListView.java:1350)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1419)
at android.widget.ListView.onMeasure(ListView.java:1323)
at android.view.View.measure(View.java:25532)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:25532)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:25532)
at androidx.recyclerview.widget.RecyclerView$LayoutManager.measureChildWithMargins(RecyclerView.java:9384)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1653)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
at android.view.View.layout(View.java:22900)
at android.view.ViewGroup.layout(ViewGroup.java:6389)
at androidx.viewpager2.widget.ViewPager2.onLayout(ViewPager2.java:527)
at android.view.View.layout(View.java:22900)
at android.view.ViewGroup.layout(ViewGroup.java:6389)
at com.google.android.material.appbar.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:148)
at com.google.android.material.appbar.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:43)
at com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:2003)
at androidx.coordinatorlayout.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:918)
at android.view.View.layout(View.java:22900)
at android.view.ViewGroup.layout(ViewGroup.java:6389)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:22900)
at android.view.ViewGroup.layout(ViewGroup.java:6389)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
at android.view.View.layout(View.java:22900)
at android.view.ViewGroup.layout(ViewGroup.java:6389)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:22900)
at android.view.ViewGroup.layout(ViewGroup.java:6389)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
E/AndroidRuntime: at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
at android.view.View.layout(View.java:22900)
at android.view.ViewGroup.layout(ViewGroup.java:6389)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:831)
at android.view.View.layout(View.java:22900)
at android.view.ViewGroup.layout(ViewGroup.java:6389)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3619)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3087)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2060)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8430)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:972)
at android.view.Choreographer.doCallbacks(Choreographer.java:796)
at android.view.Choreographer.doFrame(Choreographer.java:731)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:957)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:250)
at android.app.ActivityThread.main(ActivityThread.java:7886)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:970)
所以我重新检查了我在 XML 中输入的内容,看起来还不错,但也许我忘记或弄错了什么?
谢谢!干杯!
编辑 2:
您好 @Cheticamp ,按照您的建议,我从头开始创建一个 ConstraintLayout ,其中包含两个按百分比标注的准则,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/test_left_guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.50" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/test_right_guideline"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.20"
app:layout_constraintLeft_toEndOf="@id/test_left_guideline" />
</androidx.constraintlayout.widget.ConstraintLayout>
所以它应该在设计中并排看到这两个指南,因为我app:layout_constraintLeft_toEndOf="@id/test_left_guideline"
在我可以按照这些指南将两个 RelativeLayouts 作为容器放置之后,但看起来这两个指南都从父窗口的左侧开始......我应该哪个属性设置并排制定这两个准则?提前致谢!干杯