如何从屏幕顶部显示相对布局内容

时间:2016-04-29 05:05:38

标签: android android-layout android-relativelayout

我在文件mylayout.xml中有以下布局。

expandableListview.setOnItemLongClickListener(new OnItemLongClickListener() {
      @Override
      public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
          int itemType = ExpandableListView.getPackedPositionType(id);

          if ( itemType == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
              childPosition = ExpandableListView.getPackedPositionChild(id);
              groupPosition = ExpandableListView.getPackedPositionGroup(id);

              //do your per-item callback here
              return true; //true if we consumed the click, false if not

          } else if(itemType == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
              groupPosition = ExpandableListView.getPackedPositionGroup(id);
              //do your per-group callback here
              return true; //true if we consumed the click, false if not

          } else {
              // null item; we don't consume the click
              return false;
          }
  });

使用此布局,当我为具有透明主题的活动执行setContentView(R.layout.mylayout)时,我的布局将显示在屏幕的中央。

我想动态更改此布局定位,因此中心(默认)和底部(通过使用以下代码)显示正在运行。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/yellow"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="2dp"
    android:id="@+id/Layout5"
    android:layout_alignParentTop="true"
    android:layout_gravity="top"
    android:gravity="top"
    android:orientation="vertical">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/Layout6"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:gravity="top">

    <ImageView
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:background="@color/green"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_marginTop="5dp"
        android:id="@+id/imageView"
        android:layout_gravity="center_horizontal" />

    <LinearLayout
        android:id="@+id/linear_layout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:layout_below="@+id/imageView">


        <Button
            android:id="@+id/btn1"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="@string/text_btn_1"
            />

        <Button
            android:id="@+id/btn2"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="@string/text_btn_2"
            />
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

但不知怎的,我无法弄清楚如何动态地将这个布局显示在屏幕的顶部。

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:0)

尝试删除之前制定的规则(将其设置为对齐底部),然后添加新规则:

func constructInfoViewBtns() {
    let buttonsView = UIView.init(frame: CGRectMake(0, 90, self.infoView!.frame.width, 90))

    let playPauseBtn = UIButton(type: UIButtonType.Custom) as UIButton
    let playPauseBtnImg : UIImage = UIImage(named: "pauseBtn")!
    playPauseBtn.setImage(playPauseBtnImg, forState: .Normal)
    playPauseBtn.imageView?.contentMode = UIViewContentMode.ScaleAspectFill
    playPauseBtn.frame = CGRectMake(0, 0, 55, 55)
    playPauseBtn.backgroundColor = UIColor.blueColor()
    playPauseBtn.center = CGPointMake(buttonsView.frame.width/2, buttonsView.frame.height/2)
    playPauseBtn.addTarget(self, action: "playPauseTrack:", forControlEvents: .TouchDown)
    self.playPauseBtn = playPauseBtn

    buttonsView.addSubview(self.playPauseBtn!)

    self.infoView!.addSubview(buttonsView)

}

请注意,可从API 17开始提供removeRule()。

答案 1 :(得分:0)

尝试按以下方式修改:

layout.xml

    <root>
    <celldata>
    <name>Alex Company: X</name>
    </celldata>

    <celldata>
    <name>Braun Company: Y</name>
    </celldata>

    </root>

类:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/Layout5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:background="@color/yellow"
                android:padding="2dp"
                android:paddingLeft="5dp"
                android:paddingRight="5dp">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:background="@color/green"/>

    <LinearLayout
        android:id="@+id/linear_layout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@+id/imageView"
        android:orientation="horizontal">


        <Button
            android:id="@+id/btn1"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="button1"
            />

        <Button
            android:id="@+id/btn2"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="button2"
            />
    </LinearLayout>
</RelativeLayout>

答案 2 :(得分:0)

我找到了针对此问题的以下修复程序。 我在相对布局下最后添加了填充衬里布局。          

要在中心显示,请不要做任何事情。

在顶部使用以下代码显示。只需将填充布局移至底部即可。

    LinearLayout lLayout = (LinearLayout) findViewById(R.id.filler);
    RelativeLayout.LayoutParams relativeParas = (RelativeLayout.LayoutParams) lLayout.getLayoutParams();
    relativeParas.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    lLayout.setLayoutParams(relativeParas); 

要在底部显示以下代码。即将layout6移动到底部。

    LinearLayout lLayout = (LinearLayout) findViewById(R.id.Layout6);
    RelativeLayout.LayoutParams relativeParas = (RelativeLayout.LayoutParams) lLayout.getLayoutParams();
    relativeParas.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    lLayout.setLayoutParams(relativeParas);