2017-08-16 134 views
0

在Android Studio中,使用线性布局,使用layout_weight - 我可以非常容易地解决我的问题(为每个视图设置layout_weight,我需要多少),但是我为layout_width挣扎(在一个屏幕上应用程序看起来不错,但在另一个屏幕上却不行) ,对于初学者来说,理解dp,sp,px等一点点压倒性的东西。我们可以以某种方式制作“layout_weight”而不是宽度吗?我们可以以某种方式制作“layout_weight”而不是宽度?

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
tools:context="com.example.android.myapplication3.MainActivity"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:background="#FAFAFA"> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp" 
     android:layout_weight="0" 
     android:text="The title will be here " 

//The length of this text up here is permanent, and is 23 characters, also 
//that much length for the texts in TextViews in other Linear Layouts below 
//While in all CheckBoxes in all Layouts the text is same and permanent and 
//will contain 7 characters. 
//Also i will add a onClick in ImageView. 

     android:textAppearance="@style/TextAppearance.AppCompat.Medium" 
     android:textColor="#E91E63" 
     android:textStyle="bold" /> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp" 
     android:layout_weight="0" 
     android:src="@drawable/ic_dehaze_black_24dp" /> 

    <CheckBox 
     android:id="@+id/checkBox1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="10dp" 
     android:layout_marginStart="10dp" 
     android:layout_marginTop="10dp" 
     android:layout_weight="0" 
     android:text="CheckBox1" 
     android:textColor="#E91E63" 
     android:textStyle="bold"/> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:background="#FAFAFA"> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp" 
     android:layout_weight="0" 
     android:text="The title will be here " 
     android:textAppearance="@style/TextAppearance.AppCompat.Medium" 
     android:textColor="#E91E63" 
     android:textStyle="bold" /> 

    <ImageView 
     android:id="@+id/imageView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp" 
     android:layout_weight="0" 
     android:src="@drawable/ic_dehaze_black_24dp" /> 

    <CheckBox 
     android:id="@+id/checkBox2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="10dp" 
     android:layout_marginStart="10dp" 
     android:layout_marginTop="10dp" 
     android:layout_weight="0" 
     android:text="CheckBox2" 
     android:textColor="#E91E63" 
     android:textStyle="bold"/> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:background="#FAFAFA"> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp" 
     android:layout_weight="0" 
     android:text="The title will be here " 
     android:textAppearance="@style/TextAppearance.AppCompat.Medium" 
     android:textColor="#E91E63" 
     android:textStyle="bold" /> 

    <ImageView 
     android:id="@+id/imageView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp" 
     android:layout_weight="0" 
     android:src="@drawable/ic_dehaze_black_24dp" /> 

    <CheckBox 
     android:id="@+id/checkBox3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="10dp" 
     android:layout_marginStart="10dp" 
     android:layout_marginTop="10dp" 
     android:layout_weight="0" 
     android:text="CheckBox3" 
     android:textColor="#E91E63" 
     android:textStyle="bold"/> 

</LinearLayout> 

+0

安置自己的XML,所以我们可以建议你 –

+0

学习如何使用约束布局正确的方法.. .https://medium.com/@loutry/guide-to-constraintlayout-407cd87bc013 –

+0

是的,你可以使用权重的宽度...只是weight_sum根视图和布局宽度= 0dp和分配重量 –

回答

0

是的,可以。

而不是将android:layout_height设置为零,请设置android:layout_width。工作方式与高度相同,但宽度相同。

0

为了个别组件使用layout_weight;请确保您在根标记声明中使用权重属性。

当使用layout_weight:

  1. 提供layout_width = 0dp在同一组件为水平取向
  2. layout_height = 0dp为垂直方向。
  3. 沿layout_height有值/ layout_width混合layout_weight要么wrap_contet/match_parent没有给出正确的外观
相关问题