ImageButtons没有正确排列

时间:2015-02-02 10:18:23

标签: java android

我已经按照Facebook,Instagram,Twitter和Reddit的顺序将我的按钮从上到下排列,但它没有这样做,它只显示Facebook和Reddit和我不知道为什么。 我已经让我的按钮使用以下代码自动调整活动创建大小:

    public void getScreenRes() {
    DisplayMetrics display = this.getResources().getDisplayMetrics();
    int screenwidth = display.widthPixels;
    int screenheight = display.heightPixels;
    double buttonheight = screenwidth / 2.66666667;
    int buttonheightint= (int) Math.round(buttonheight);
    ImageButton fbLogin = (ImageButton) findViewById(R.id.facebookLogin);
    ViewGroup.LayoutParams fb = fbLogin.getLayoutParams();
    fb.width = screenwidth;
    fb.height = buttonheightint;
    fbLogin.setLayoutParams(fb);
    ImageButton instaLogin = (ImageButton) findViewById(R.id.instagramLogin);
    ViewGroup.LayoutParams insta = instaLogin.getLayoutParams();
    insta.width = screenwidth;
    insta.height = buttonheightint;
    instaLogin.setLayoutParams(insta);
    ImageButton twitLogin = (ImageButton) findViewById(R.id.twitterLogin);
    ViewGroup.LayoutParams twit = instaLogin.getLayoutParams();
    twit.width = screenwidth;
    twit.height = buttonheightint;
    twitLogin.setLayoutParams(twit);
    ImageButton redditLogin = (ImageButton) findViewById(R.id.redditLogin);
    ViewGroup.LayoutParams reddit = instaLogin.getLayoutParams();
    reddit.width = screenwidth;
    reddit.height = buttonheightint;
    redditLogin.setLayoutParams(reddit);
}

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="16dp"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.eren.valour.FirstTimeLogin">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/getStarted"
    android:id="@+id/getStartedText"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
   />
   <ImageButton
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_marginTop="100dp"
       android:background="@drawable/facebook"
       android:id="@+id/facebookLogin"
       android:onClick="facebookLogin"

       />
   <ImageButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/instagram"
        android:layout_below="@+id/facebookLogin"
        android:id="@+id/instagramLogin"
        android:onClick="instagramLogin"
        />
    <ImageButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/twitter"
        android:layout_below="@+id/instagramLogin"
        android:id="@+id/twitterLogin"
        android:onClick="twitterLogin"
        />

    <ImageButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/reddit"
        android:layout_below="@+id/twitterLogin"
        android:id="@+id/redditLogin"
        android:onClick="redditLogin"
        />

现在的样子:

4 个答案:

答案 0 :(得分:1)

您需要获取相应的Layoutparams。

ViewGroup.LayoutParams reddit = instaLogin.getLayoutParams();

应该是

ViewGroup.LayoutParams reddit = redditLogin.getLayoutParams();

与Twitter登录相同:

 ViewGroup.LayoutParams twit = twitLogin.getLayoutParams();

你正在做的是将instagram layoutparams应用到3个按钮,这样它们就可以放在另一个按钮的顶部,所以只有最后的节目。可能只是副本+过去的错误,但这应该解决它。

如果没有解决问题,问题应该在布局文件中。尝试使用LinearLayout或为RelativeLayout设置正确的属性。

答案 1 :(得分:0)

尝试将布局从RelativLayout更改为LinearLayout。应该这样做。

答案 2 :(得分:0)

您可以将它放入具有权重的LinearLayout中。没有java方法。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
          android:layout_height="match_parent" android:orientation="vertical"
          android:paddingTop="16dp" android:weightSum="5"
          tools:context="com.eren.valour.FirstTimeLogin">

<TextView
    android:id="@+id/getStartedText" android:layout_width="match_parent"
    android:layout_height="wrap_content" android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" android:layout_weight="1" android:padding="50dp" // you can tweak this figure here
    android:text="getStarted\nhere" android:textAppearance="?android:attr/textAppearanceLarge"/>

<ImageButton
    android:id="@+id/facebookLogin" android:layout_width="match_parent"
    android:layout_height="wrap_content" android:layout_weight="1"
    android:background="@drawable/fb" android:onClick="facebookLogin"/>

<ImageButton
    android:id="@+id/instagramLogin" android:layout_width="match_parent"
    android:layout_height="wrap_content" android:layout_weight="1"
    android:background="@drawable/insta" android:onClick="instagramLogin"/>

<ImageButton
    android:id="@+id/twitterLogin" android:layout_width="match_parent"
    android:layout_height="wrap_content" android:layout_weight="1"
    android:background="@drawable/twit" android:onClick="twitterLogin"/>

<ImageButton
    android:id="@+id/redditLogin" android:layout_width="match_parent"
    android:layout_height="wrap_content" android:layout_weight="1"
    android:background="@drawable/reddit" android:onClick="redditLogin"/>
</LinearLayout>

view

答案 3 :(得分:0)

更新您的XMl .. 而不是RelativeLayout,我认为你应该像这样使用LinearLayout和Weight属性......

      <LineaLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:paddingTop="16dp"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.eren.valour.FirstTimeLogin"
    android:orientation="vertical" >


    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/getStarted"
        android:id="@+id/getStartedText"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
    android:layout_weight="1"
       />
       <ImageButton
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_marginTop="100dp"
           android:background="@drawable/facebook"
           android:id="@+id/facebookLogin"
           android:onClick="facebookLogin"
    android:layout_weight="1"

           />
       <ImageButton
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/instagram"
            android:layout_below="@+id/facebookLogin"
            android:id="@+id/instagramLogin"
            android:onClick="instagramLogin"
    android:layout_weight="1"
            />
        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/twitter"
            android:layout_below="@+id/instagramLogin"
            android:id="@+id/twitterLogin"
            android:onClick="twitterLogin"
    android:layout_weight="1"
            />

        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/reddit"
            android:layout_below="@+id/twitterLogin"
            android:id="@+id/redditLogin"
            android:onClick="redditLogin"
android:layout_weight="1"
            />

此外,您不再需要此方法getScreenRes()

相关问题