2011-07-22 95 views
35

大家好我只是在LinearLayout中实现了一个ListView,但我需要定义LinearLayout的高度(它必须是屏幕高度的50%)。android:layout_height屏幕大小的50%

<LinearLayout 
    android:id="@+id/widget34" 
    android:layout_width="300px" 
    android:layout_height="235px" 
    android:orientation="vertical" 
    android:layout_below="@+id/tv_scanning_for" 
    android:layout_centerHorizontal="true"> 

    <ListView 
     android:id="@+id/lv_events" 
     android:textSize="18sp"   
     android:cacheColorHint="#00000000" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/tv_scanning_for" 
     android:layout_centerHorizontal="true"> 
    </ListView> 

</LinearLayout> 

那可能吗?

我为按钮和EditText做了类似的事情,但似乎不适用于布局。

这是我的代码:

//capture the size of the devices screen 
    Display display = getWindowManager().getDefaultDisplay(); 
    double width = display.getWidth(); 

    //my EditText will be smaller than full screen (80%)   
    double doubleSize = (width/5)*4; 
    int editTextSize = (int) doubleSize; 

    //define the EditText 
    userName = (EditText) this.findViewById(R.id.userName); 
    password = (EditText) this.findViewById(R.id.password); 

    //set the size 
    userName.setWidth(editTextSize); 
    password.setWidth(editTextSize); 

谢谢! :)

+0

您是否尝试过在LinearLayout周围使用RelativeLayout并设置RelativeLayout的大小? –

+0

我可以在RelativeLayout高度参数中输入百分比吗? –

+0

使用自定义LayoutParams:http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html与RelativeLayout.setLayoutParams():http://developer.android.com/reference/android/view/View的.html#setLayoutParams%28android.view.ViewGroup.LayoutParams%29。设置高度为display.getHeight()/ 2 –

回答

70

设置其layout_height="0dp" *,添加它下方的空白View(或空白ImageView或只是一个FrameLayout)与layout_height也等于0dp,并设置两种观点有layout_weight="1"

这将每个伸展因为它填充屏幕同样地查看。由于两者具有相同的重量,每个将占用屏幕的50%。

*请参阅adamp的评论,了解其中的原因以及其他真正有用的小技巧。

+0

这正是我如何做到这一点。 – Rob

+17

当你使用体重时,请记住在测量完所有子体视图后,体重会分配*剩余空间。如果你只想依靠体重,你需要设置'layout_height =“0dp”'而不是'wrap_content'。你也可以直接在LinearLayout上设置'weightSum',而不是添加空白视图来填充总重量。 – adamp

+0

@adamp这是我不知道的非常优秀的信息。非常感谢。 – LeffelMania

6

这很容易在xml中完成。将顶层容器设置为LinearLayout并根据需要设置orientation属性。然后在那里放置两个在宽度和高度都有“填充父母”的线性布局。最后,这两个linearlayouts的weigth属性设置为1。

0

你应该做这样的事情:

<LinearLayout 
    android:id="@+id/widget34" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:layout_below="@+id/tv_scanning_for" 
    android:layout_centerHorizontal="true"> 

    <ListView 
     android:id="@+id/lv_events" 
     android:textSize="18sp"   
     android:cacheColorHint="#00000000" 
     android:layout_height="1" 
     android:layout_width="fill_parent" 
     android:layout_weight="0dp" 
     android:layout_below="@+id/tv_scanning_for" 
     android:layout_centerHorizontal="true" 
     /> 

</LinearLayout> 

还可以使用DP代替PX或读到它here

+2

交换你的身高和体重值,这将是正确的。 ;) – adamp

+0

是的,这将是正确的,但仅适用于当前的设备分辨率。如果测试使用不同的分辨率怎么办?然后根据新的显示分辨率,他将获得或多或少的50%!这对于高度/宽度的静态值来说有点棘手!了解如何使用UI组件的比例尺寸! – nenito

+0

重量和身高仍然有交换价值。 –

5

这是我的机器人:layout_height = 50% 活动:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:id="@+id/alipay_login" 
     style="@style/loginType" 
     android:background="#27b" > 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/taobao_login" 
     style="@style/loginType" 
     android:background="#ed6d00" > 
    </LinearLayout> 

</LinearLayout> 

风格:

<style name="loginType"> 
    <item name="android:layout_width">match_parent</item> 
    <item name="android:layout_height">match_parent</item> 
    <item name="android:layout_weight">0.5</item> 
    <item name="android:orientation">vertical</item> 
</style> 
0

它是那么容易,如果你想垂直分割屏幕两部分(TOP30%+ bottom70% )

<LinearLayout 
      android:id="@+id/LinearLayoutTop" 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="2"> 

    </LinearLayout> 
    <LinearLayout 
      android:id="@+id/LinearLayoutBottom" 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1"> 

    </LinearLayout> 
3

最好的方法是使用

layout_height = “0dp” layout_weight = “0.5”

例如

<WebView 
    android:id="@+id/wvHelp" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.5" /> 

<TextView 
    android:id="@+id/txtTEMP" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.5" 
    android:text="TextView" /> 

web视图,TextView的具有屏幕高度的50%

0

要确保的图的高度为屏幕的50%,然后我们可以在LinearLayout中创建两个子LinearLayout。每个子LinearLayout应该有0的“android:layout_weight”。5覆盖屏幕一半

父LinearLayout中应该有 “安卓方向” 设置为垂直

这里,供大家参考代码.... 这个代码包含高度的两个按钮半个屏幕

<LinearLayout 
android:orientation="vertical" 
android:layout_height="match_parent"> 

<LinearLayout 

    android:layout_weight="0.5" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <Button 
     android:padding="10dp" 
     android:layout_weight="0.5" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:text="button1" 
     android:id="@+id/button1" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentBottom="true" 
     /> 

    <Button 
     android:padding="10dp" 
     android:layout_weight="0.5" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:text="button2" 
     android:id="@+id/button2" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentBottom="true" 
     /> 

    </LinearLayout> 

<LinearLayout 

    android:layout_weight="0.5" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 


    </LinearLayout> 
    </LinearLayout> 
0

这种为我工作。 尽管FAB不能独立浮动,但现在它不会被推下。

观察的LinearLayout内给予

<LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:id="@+id/andsanddkasd"> 

      <android.support.v7.widget.RecyclerView 
       android:id="@+id/sharedResourcesRecyclerView" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight="4" 
       /> 

      <android.support.design.widget.FloatingActionButton 
       android:id="@+id/fab" 
       android:layout_width="wrap_content" 
       android:layout_height="0dp" 
       android:layout_gravity="bottom|right" 
       android:src="@android:drawable/ic_input_add" 
       android:layout_weight="1"/> 

     </LinearLayout> 

希望这有助于:)

0

为了实现这一壮举,具有weightSum={amount of weight to distribute}定义外线性布局的权重。

它定义了最大权重和。如果未指定,则通过添加所有孩子的layout_weight来计算总和。例如,可以通过给它一个0.5的layout_weight并将weightSum设置为1.0来给单个孩子50%的总可用空间。另一个例子是将weightSum设置为2,并且如果两个孩子设置了layout_weight=1然后每个将获得50%的可用空间。

WeightSum取决于父级布局中子级的数量。

+0

这种情况下的孩子可以是任何视图组(FrameLayout)或任何视图,如imageView等 – Remario