滚动的LinearLayout与重量大于屏幕的android [英] scrollable linearlayout with weights bigger than screen in android

查看:190
本文介绍了滚动的LinearLayout与重量大于屏幕的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建与重量,有一个大小比屏幕更大的垂直的LinearLayout。比方说,2倍的屏幕的大小。为了使这个工作,我显然需要能够滚动它。不幸的是,我不能想出一个办法做到这一点。我尝试使用的布局权重,并设置权重总和一半分量的权重的实际总和(因此,如果所有部件的重量之和为20我设置的权重之和为10),并设法使之工作,但不幸的是,滚动不工作了的某些原因。

这有什么,我很想念?

这是在code,使的LinearLayout两倍大屏幕,但滚动不工作:

 <滚动型的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_height =match_parent
    机器人:layout_width =match_parent
    机器人:fillViewport =真正的>

    <的LinearLayout
        机器人:方向=垂直
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:weightSum =2>

        < EditText上机器人:ID =@ + ID / ID1
            机器人:layout_width =match_parent
            机器人:layout_height =0dp
            机器人:TEXTSIZE =25dp
            机器人:重力=中心
            机器人:layout_weight =2/>

        < EditText上机器人:ID =@ + ID / ID2
            机器人:layout_width =match_parent
            机器人:layout_height =0dp
            机器人:TEXTSIZE =25dp
            机器人:重力=中心
            机器人:layout_weight =2/>

    < / LinearLayout中>
< /滚动型>
 

解决方案

根据您的评论,这里是实现你要查找的内容的编程方法。我不相信有一种方法可以做到这一点的纯布局XML。

布局:

 <滚动型的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:layout_gravity =center_horizo​​ntal
    机器人:后台=#000000
    机器人:fillViewport =真正的>

    <的LinearLayout
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:后台=#FFFFFF
        机器人:方向=垂直>

        <的EditText
            机器人:ID =@ + ID / ID1
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:后台=#DCDCDC
            机器人:重力=中心
            机器人:文本=一一一
            机器人:textIsSelectable =假
            机器人:TEXTSIZE =45SP/>

        <的EditText
            机器人:ID =@ + ID / ID2
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:后台=#AAAAAA
            机器人:重力=中心
            机器人:文本=TWO TWO TWO
            机器人:textIsSelectable =假
            机器人:TEXTSIZE =45SP/>

        <的EditText
            机器人:ID =@ + ID / ID3
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:后台=#777777
            机器人:重力=中心
            机器人:文本=三三三
            机器人:textIsSelectable =假
            机器人:TEXTSIZE =45SP/>
    < / LinearLayout中>

< /滚动型>
 

活动:

 公共无效的onCreate(包savedInstanceState){
      super.onCreate(savedInstanceState);
      的setContentView(R.layout.main);

      //获取显示尺寸 -  API L13及以上。否则,使用getResources()getDisplayMetrics()。
      显示显示= getWindowManager()getDefaultDisplay()。
      点大小=新的点();
      display.getSize(大小);


      //你想要的大小为每50的EditText%,所以除以2可用高度。
      //注:这是绝对的高度,不考虑窗口装饰!
      INT editTextHeight = size.y / 2;

      //得到一个处理您的EditTexts
      的EditText T1 =(EditText上)findViewById(R.id.id1);
      的EditText T2 =(EditText上)findViewById(R.id.id2);
      的EditText T3 =(EditText上)findViewById(R.id.id3);

      //设置高度屏幕大小各50%
      t1.setHeight(editTextHeight);
      t2.setHeight(editTextHeight);
      t3.setHeight(editTextHeight);
}
 

这会做到这一点。最终结果是:

I am trying to create a vertical linearlayout with weights that has a size bigger than the screen. Let's say 2x the size of the screen. In order for this to work I would obviously need to be able to scroll through it. Unfortunately I can't figure out a way to do this. I tried using the layout weights, and setting the weight sum as half of the actual sum of the weights of all components (so if all components weights sum is 20 I set the weight sum as 10) and managed to make it work but unfortunately the scrolling is not working anymore for some reason.

Is there anything that I am missing?

this is the code that makes the linearlayout twice as big as the screen but the scroll is not working:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:fillViewport="true">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="2">

        <EditText android:id="@+id/id1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:textSize="25dp"
            android:gravity="center"
            android:layout_weight="2"/>

        <EditText android:id="@+id/id2"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:textSize="25dp"
            android:gravity="center"
            android:layout_weight="2"/>

    </LinearLayout>
</ScrollView>

解决方案

Based on your comment, here is a programmatic way of achieving what you're looking for. I do not believe there is a way to accomplish this in pure layout XML.

Layout:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    android:background="#000000"
    android:fillViewport="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/id1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#DCDCDC"
            android:gravity="center"
            android:text="ONE ONE ONE"
            android:textIsSelectable="false"
            android:textSize="45sp" />

        <EditText
            android:id="@+id/id2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#AAAAAA"
            android:gravity="center"
            android:text="TWO TWO TWO"
            android:textIsSelectable="false"
            android:textSize="45sp" />

        <EditText
            android:id="@+id/id3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#777777"
            android:gravity="center"
            android:text="THREE THREE THREE"
            android:textIsSelectable="false"
            android:textSize="45sp" />
    </LinearLayout>

</ScrollView>

Activity:

public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      // Get display size -- API L13 and up. Otherwise use getResources().getDisplayMetrics();
      Display display = getWindowManager().getDefaultDisplay();
      Point size = new Point();
      display.getSize(size);


      // You want size to be 50% per EditText, so divide available height by 2.
      // Note: this is absolute height, does not take into consideration window decoration!
      int editTextHeight = size.y / 2;

      // Get a handle to your EditTexts
      EditText t1 = (EditText) findViewById(R.id.id1);
      EditText t2 = (EditText) findViewById(R.id.id2);
      EditText t3 = (EditText) findViewById(R.id.id3);

      // Set height to 50% of screen size each
      t1.setHeight(editTextHeight);
      t2.setHeight(editTextHeight);
      t3.setHeight(editTextHeight);
}

That'll do it. End result:

这篇关于滚动的LinearLayout与重量大于屏幕的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆