2017-06-04 40 views
0

我想在屏幕上集中RecyclerView列表。它将成为一个单子,所以我将使用LinearLayoutManager(LLM)。 但是使用其不集中的项目的LLM,和它看起来像这样的风景模式: Landscape mode with LLM使用LinearLayoutManager集中RecyclerView

的XML代码如下所示:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" android:layout_width="match_parent" 

    android:layout_height="match_parent"> 

    <android.support.v7.widget.CardView 
     android:id="@+id/recipe_card" 
     android:layout_gravity="center" 
     android:layout_width="400dp" 
     android:layout_height="186dp" 
     style="@style/CardViewStyle"> 

     <android.support.constraint.ConstraintLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 


      <ImageView 
       android:id="@+id/recipe_image" 
       android:layout_width="120dp" 
       android:layout_height="163dp" 
       app:srcCompat="@mipmap/ic_pan" 
       app:layout_constraintTop_toTopOf="parent" 
       android:layout_marginTop="8dp" 
       app:layout_constraintBottom_toBottomOf="parent" 
       android:layout_marginBottom="8dp" 
       android:layout_marginLeft="8dp" 
       app:layout_constraintLeft_toLeftOf="parent" 
       tools:layout_editor_absoluteY="3dp" /> 

      <TextView 
       android:id="@+id/recipe_name" 
       android:layout_width="250dp" 
       android:layout_height="119dp" 
       android:text="TextView" 
       app:layout_constraintLeft_toRightOf="@+id/recipe_image" 
       android:layout_marginLeft="8dp" 
       app:layout_constraintTop_toTopOf="parent" 
       android:layout_marginTop="16dp" /> 

      <TextView 
       android:id="@+id/recipe_servings" 
       android:layout_width="164dp" 
       android:layout_height="32dp" 
       android:text="TextView" 
       android:layout_marginTop="8dp" 
       app:layout_constraintTop_toBottomOf="@+id/recipe_name" 
       app:layout_constraintBottom_toBottomOf="parent" 
       android:layout_marginBottom="8dp" 
       app:layout_constraintLeft_toRightOf="@+id/recipe_image" 
       android:layout_marginLeft="94dp" 
       app:layout_constraintVertical_bias="0.0" /> 
     </android.support.constraint.ConstraintLayout> 

    </android.support.v7.widget.CardView> 

</FrameLayout> 

在Android Studio的预览,它看起来不错,但在应用程序,它不。 我设法通过只更改一列GridLayoutManager的LLM来修复它。但它看起来很丑。

有谁知道发生了什么事? 谢谢。

+0

大卫检查我的回答 – scienticious

+0

大卫·我的回答能帮助ü大大无处不在你检查一下吗? – pouya

回答

0

尝试将主布局的内容重心设置为居中。有两种方法可以做到这一点:

  1. 将“centerHorozontal”属性添加到您的框架布局的“true”。
  2. 将您的框架布局更改为线性布局,并将主线性布局的重力设置为“中心”。

希望它能起作用,如果它没有发送给我你正在布局中使用的图像,我会尝试使用其他一些技术来修复它,并会向您发送更新的代码。

0

与相对布局替换约束布局

然后,使图像是中央,使用此

<ImageView 
     android:id="@+id/image" 
     android:layout_width="match_parent" 
     android:layout_height="100dp" 
     android:clickable="true" 
     android:scaleType="centerCrop" /> 

然后也使各份文本是中央

android:textAlignment="center" 
0

请检查您的如果你这样写:

@Override 
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     return new ViewHolder(mLayoutInflater.inflate(R.layout.your_item_layout, null)); 
} 

这将使您的FrameLayout被禁用,CardView将成为您ViewHolder的itemView,所以您应该改变如下: @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new ViewHolder(mLayoutInflater.inflate(R.layout.your_item_layout, parent, false)); } 希望帮助你。

0

如果我是你我会得到我的显示器尺寸宽度,并相应地设置我的ViewHolder一些保证金。

这是我的代码在我的应用程序类:

public class MyApp extends Application { 
public static Point displaySize = new Point(); 
public static float density = 1; 
public static Context appContext = null; 
public static int height; 

@Override 
public void onCreate() { 
    super.onCreate(); 
    ActiveAndroid.initialize(this); 
    appContext = getApplicationContext(); 
    checkDisplaySize(); 
    density = appContext.getResources().getDisplayMetrics().density; 
} 

public static void checkDisplaySize() { 
    try { 
     WindowManager manager = (WindowManager) 
appContext.getSystemService(Context.WINDOW_SERVICE); 
     if (manager != null) { 
      Display display = manager.getDefaultDisplay(); 
      if (display != null) { 
        display.getSize(displaySize); 
      } 
      height = (int) (displaySize.x/1.777f); //Aspect Ratio 16:9 
     } 
    } catch (Exception e) { 
    } 
} 
} 

毕竟这在适配器中,你得到的持有者只查看做到以下几点:

int marginWidth = (MyApp.desplaySize.x - itemView.getMeasuredWidth())/2; 
LayoutParams params = new LayoutParams(
    LayoutParams.WRAP_CONTENT,  
    LayoutParams.WRAP_CONTENT 
); 
params.setMargins(marginWidth, 0, marginWidth, 0); 
itemView.setLayoutParams(params); 

我真的不知道这确切的代码将解决你的问题,但你可以很容易地改变这一点,所以它会。

0

写这段代码在你的FrameLayout

android:layout_gravity="center" 
相关问题