使用主题对话框在特定的x,y位置定位活动

时间:2011-09-19 23:18:29

标签: android android-layout android-dialog

我想将对话框放在屏幕上的特定位置(距顶部-10px,距离let-510px)。

我确实应用了主题并添加了android:scrollXandroid:scrollY,但它似乎无效。

任何解决方案?

下面是我的造型xml。

<style name="Theme.MyDialog1" parent="android:style/Theme.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:layout_width">300dp</item>
    <item name="android:scrollX">-10dp</item>
    <item name="android:scrollY">-5dp</item>
</style>

编辑:

我还尝试了android:layout_xandroid:layout_y,但没有用!

这是我拥有的 ![在此输入图像说明] [1]

但我真正想要的是位于特定x和y正面的注册徽标正下方!!!!! [在此输入图像说明] [2]

2 个答案:

答案 0 :(得分:4)

这是代码!

LayoutParams lp = this.getWindow().getAttributes();
             lp.x=50;lp.y=20;lp.width=100;lp.height=200;lp.gravity=Gravity.TOP | Gravity.LEFT;
            lp.dimAmount=0;            
            lp.flags=LayoutParams.FLAG_LAYOUT_NO_LIMITS | LayoutParams.FLAG_NOT_TOUCH_MODAL;
this.setContentView(view, lp);

答案 1 :(得分:1)

不要扩展对话框主题扩展面板主题。

<style name="Theme.CustomPanel" parent="@android:style/Theme.Panel">        
         <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
         <item name="android:backgroundDimEnabled">true</item>
    </style>

这会创建一个类似于对话框的窗口,除了它不像对话框那样居中。然后像平常一样创建一个活动然后启动它。

要使其从左侧5px,从顶部10px,只需在布局上填充

 <!-- res/layout/your_layout.xml -->
   <FrameLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:paddingTop="10px"
  android:paddingLeft="5px"></FrameLayout>

当然,您的活动应该像

一样正常
public class NonCenteredDialogActivity extends Activity{
      protected void onCreate(Bundle bundle){
          super.onCreate(bundle);
          setContentView(R.layout.your_layout);
      }
    }